/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
67 строк
2 KB
Samuel Facchinello
style: enable `InvalidJavadocPosition` in checkstyle (#5237)
18 июн 2024, 20:34
Не верифицирован
18 июн 2024, 20:34
74e5199
Код
Авторство
О чём код?
package com.thealgorithms.maths; import java.math.BigInteger; /** * <a href="https://en.wikipedia.org/wiki/Automorphic_number">Automorphic Number</a> * A number is said to be an Automorphic, if it is present in the last digit(s) * of its square. Example- Let the number be 25, its square is 625. Since, * 25(The input number) is present in the last two digits of its square(625), it * is an Automorphic Number. */ public final class AutomorphicNumber { private AutomorphicNumber() { } /** * A function to check if a number is Automorphic number or not * * @param n The number to be checked * @return {@code true} if {@code a} is Automorphic number, otherwise * {@code false} */ public static boolean isAutomorphic(long n) { if (n < 0) { return false; } long square = n * n; // Calculating square of the number long t = n; long numberOfdigits = 0; while (t > 0) { numberOfdigits++; // Calculating number of digits in n t /= 10; } long lastDigits = square % (long) Math.pow(10, numberOfdigits); // Extracting last Digits of square return n == lastDigits; } /** * A function to check if a number is Automorphic number or not by using String functions * * @param n The number to be checked * @return {@code true} if {@code a} is Automorphic number, otherwise * {@code false} */ public static boolean isAutomorphic2(long n) { if (n < 0) { return false; } long square = n * n; // Calculating square of the number return String.valueOf(square).endsWith(String.valueOf(n)); } /** * A function to check if a number is Automorphic number or not by using BigInteger * * @param s The number in String to be checked * @return {@code true} if {@code a} is Automorphic number, otherwise * {@code false} */ public static boolean isAutomorphic3(String s) { BigInteger n = new BigInteger(s); if (n.signum() == -1) { return false; // if number is negative, return false } BigInteger square = n.multiply(n); // Calculating square of the number return String.valueOf(square).endsWith(String.valueOf(n)); } }