Add AutomorphicNumber (#3735)

This commit is contained in:
Taranjeet Singh Kalsi
2022-11-03 18:25:48 +05:30
committed by GitHub
parent dfe733f777
commit 37db41fd6b
2 changed files with 59 additions and 44 deletions

View File

@ -1,57 +1,63 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
/** /**
* Wikipedia link for Automorphic Number : https://en.wikipedia.org/wiki/Automorphic_number
* A number is said to be an Automorphic, if it is present in the last digit(s) * 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, * 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 * 25(The input number) is present in the last two digits of its square(625), it
* is an Automorphic Number. * is an Automorphic Number.
*/ */
import java.io.*;
import java.math.BigInteger;
public class AutomorphicNumber { public class AutomorphicNumber {
//returns True if the number is a Automorphic number and False if it is not an Automorphic number /**
public static boolean isAutomorphic(int n) { * A function to check if a number is Automorphic number or not
int m, c, r, p, k; *
c = 0; * @param n The number to be checked
/** * @return {@code true} if {@code a} is Automorphic number, otherwise
* m = Temporary variable to store a copy of the number entered by the * {@code false}
* user. n = The number entered by the user c = Count the digits of the */
* number entered by user. p = To calculate the square of the number. k public static boolean isAutomorphic(long n) {
* = Support variable to count the digits of the number if (n < 0)
*/
double s;
m = n;
p = m * m; //Calculating square of the number
do {
k = n / 10;
c = c + 1; //Counting the digits of the number entered by user.
n = k;
} while (n != 0);
s = Math.pow(10, c);
r = p % (int) s;
if (m == r) { //Checking if the original number entered is present at the end of the square
return true;
} else {
return false; return false;
long square = n * n; // Calculating square of the number
long t = n, 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;
} }
/** /**
* Method to check if number is Automorphic Number or Not 1) Input - Enter a * A function to check if a number is Automorphic number or not by using String functions
* Number: 25 Output - It is an Automorphic Number. 2) Input - Enter a *
* Number: 7 Output - It is not an Automorphic Number. * @param n The number to be checked
* @return {@code true} if {@code a} is Automorphic number, otherwise
* {@code false}
*/ */
public static void main(String args[]) throws IOException { public static boolean isAutomorphic2(long n) {
BufferedReader br = new BufferedReader( if (n < 0)
new InputStreamReader(System.in) return false;
); long square = n * n; // Calculating square of the number
System.out.println("Enter a Number: "); return String.valueOf(square).endsWith(String.valueOf(n));
int n = Integer.parseInt(br.readLine()); }
if (isAutomorphic(n)) {
System.out.println("It is an Automorphic Number."); /**
} else { * A function to check if a number is Automorphic number or not by using BigInteger
System.out.println("It is not an Automorphic Number."); *
} * @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));
} }
} }

View File

@ -1,16 +1,25 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class AutomorphicNumberTest { public class AutomorphicNumberTest {
@Test @Test
void testAutomorphicNumber() { void testAutomorphicNumber() {
assertThat(AutomorphicNumber.isAutomorphic(625)).isTrue(); int trueTestCases[] = { 0, 1, 25, 625, 12890625};
assertThat(AutomorphicNumber.isAutomorphic(144)).isFalse(); int falseTestCases[] = { -5, 2, 26, 1234 };
assertThat(AutomorphicNumber.isAutomorphic(9376)).isTrue(); for (Integer n : trueTestCases) {
assertThat(AutomorphicNumber.isAutomorphic(169)).isFalse(); assertTrue(AutomorphicNumber.isAutomorphic(n));
assertTrue(AutomorphicNumber.isAutomorphic2(n));
assertTrue(AutomorphicNumber.isAutomorphic3(String.valueOf(n)));
}
for (Integer n : falseTestCases) {
assertFalse(AutomorphicNumber.isAutomorphic(n));
assertFalse(AutomorphicNumber.isAutomorphic2(n));
assertFalse(AutomorphicNumber.isAutomorphic3(String.valueOf(n)));
}
assertTrue(AutomorphicNumber.isAutomorphic3("59918212890625")); // Special case for BigInteger
assertFalse(AutomorphicNumber.isAutomorphic3("12345678912345")); // Special case for BigInteger
} }
} }