Skip to content

Commit 37db41f

Browse files
Add AutomorphicNumber (TheAlgorithms#3735)
1 parent dfe733f commit 37db41f

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,63 @@
11
package com.thealgorithms.maths;
22

33
/**
4+
* Wikipedia link for Automorphic Number : https://en.wikipedia.org/wiki/Automorphic_number
45
* A number is said to be an Automorphic, if it is present in the last digit(s)
56
* of its square. Example- Let the number be 25, its square is 625. Since,
67
* 25(The input number) is present in the last two digits of its square(625), it
78
* is an Automorphic Number.
89
*/
9-
import java.io.*;
10+
11+
import java.math.BigInteger;
1012

1113
public class AutomorphicNumber {
1214

13-
//returns True if the number is a Automorphic number and False if it is not an Automorphic number
14-
public static boolean isAutomorphic(int n) {
15-
int m, c, r, p, k;
16-
c = 0;
17-
/**
18-
* m = Temporary variable to store a copy of the number entered by the
19-
* user. n = The number entered by the user c = Count the digits of the
20-
* number entered by user. p = To calculate the square of the number. k
21-
* = Support variable to count the digits of the number
22-
*/
23-
double s;
24-
m = n;
25-
p = m * m; //Calculating square of the number
26-
do {
27-
k = n / 10;
28-
c = c + 1; //Counting the digits of the number entered by user.
29-
n = k;
30-
} while (n != 0);
31-
s = Math.pow(10, c);
32-
r = p % (int) s;
33-
if (m == r) { //Checking if the original number entered is present at the end of the square
34-
return true;
35-
} else {
15+
/**
16+
* A function to check if a number is Automorphic number or not
17+
*
18+
* @param n The number to be checked
19+
* @return {@code true} if {@code a} is Automorphic number, otherwise
20+
* {@code false}
21+
*/
22+
public static boolean isAutomorphic(long n) {
23+
if (n < 0)
3624
return false;
25+
long square = n * n; // Calculating square of the number
26+
long t = n, numberOfdigits = 0;
27+
while (t > 0) {
28+
numberOfdigits++; // Calculating number of digits in n
29+
t /= 10;
3730
}
31+
long lastDigits = square % (long) Math.pow(10, numberOfdigits); // Extracting last Digits of square
32+
return n == lastDigits;
3833
}
3934

4035
/**
41-
* Method to check if number is Automorphic Number or Not 1) Input - Enter a
42-
* Number: 25 Output - It is an Automorphic Number. 2) Input - Enter a
43-
* Number: 7 Output - It is not an Automorphic Number.
36+
* A function to check if a number is Automorphic number or not by using String functions
37+
*
38+
* @param n The number to be checked
39+
* @return {@code true} if {@code a} is Automorphic number, otherwise
40+
* {@code false}
4441
*/
45-
public static void main(String args[]) throws IOException {
46-
BufferedReader br = new BufferedReader(
47-
new InputStreamReader(System.in)
48-
);
49-
System.out.println("Enter a Number: ");
50-
int n = Integer.parseInt(br.readLine());
51-
if (isAutomorphic(n)) {
52-
System.out.println("It is an Automorphic Number.");
53-
} else {
54-
System.out.println("It is not an Automorphic Number.");
55-
}
42+
public static boolean isAutomorphic2(long n) {
43+
if (n < 0)
44+
return false;
45+
long square = n * n; // Calculating square of the number
46+
return String.valueOf(square).endsWith(String.valueOf(n));
47+
}
48+
49+
/**
50+
* A function to check if a number is Automorphic number or not by using BigInteger
51+
*
52+
* @param s The number in String to be checked
53+
* @return {@code true} if {@code a} is Automorphic number, otherwise
54+
* {@code false}
55+
*/
56+
public static boolean isAutomorphic3(String s) {
57+
BigInteger n = new BigInteger(s);
58+
if (n.signum() == -1)
59+
return false; //if number is negative, return false
60+
BigInteger square = n.multiply(n); // Calculating square of the number
61+
return String.valueOf(square).endsWith(String.valueOf(n));
5662
}
5763
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package com.thealgorithms.maths;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
3+
import static org.junit.jupiter.api.Assertions.*;
54
import org.junit.jupiter.api.Test;
65

76
public class AutomorphicNumberTest {
87

98
@Test
109
void testAutomorphicNumber() {
11-
assertThat(AutomorphicNumber.isAutomorphic(625)).isTrue();
12-
assertThat(AutomorphicNumber.isAutomorphic(144)).isFalse();
13-
assertThat(AutomorphicNumber.isAutomorphic(9376)).isTrue();
14-
assertThat(AutomorphicNumber.isAutomorphic(169)).isFalse();
10+
int trueTestCases[] = { 0, 1, 25, 625, 12890625};
11+
int falseTestCases[] = { -5, 2, 26, 1234 };
12+
for (Integer n : trueTestCases) {
13+
assertTrue(AutomorphicNumber.isAutomorphic(n));
14+
assertTrue(AutomorphicNumber.isAutomorphic2(n));
15+
assertTrue(AutomorphicNumber.isAutomorphic3(String.valueOf(n)));
16+
}
17+
for (Integer n : falseTestCases) {
18+
assertFalse(AutomorphicNumber.isAutomorphic(n));
19+
assertFalse(AutomorphicNumber.isAutomorphic2(n));
20+
assertFalse(AutomorphicNumber.isAutomorphic3(String.valueOf(n)));
21+
}
22+
assertTrue(AutomorphicNumber.isAutomorphic3("59918212890625")); // Special case for BigInteger
23+
assertFalse(AutomorphicNumber.isAutomorphic3("12345678912345")); // Special case for BigInteger
1524
}
1625
}

0 commit comments

Comments
 (0)