Repository: TheAlgorithms/Java
Pull request #3735 (2022-10-29 13:31:14)

Added functions in AutomorphicNumber.java
<!-- For completed items, change [ ] to [x] -->
  Added functions in AutomorphicNumber.java and refactored code
  Added JUnit Tests for new functions in AutomorphicNumberTest.java
- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java b/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
index 61bdd32f..3ea898cc 100644
--- a/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
+++ b/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
@@ -1,57 +1,63 @@
 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)
  * 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.
  */
-import java.io.*;
+
+import java.math.BigInteger;
 
 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) {
-        int m, c, r, p, k;
-        c = 0;
-        /**
-         * m = Temporary variable to store a copy of the number entered by the
-         * 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
-         * = Support variable to count the digits of the number
-         */
-        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 {
+    /**
+     * 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, 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
-     * Number: 25 Output - It is an Automorphic Number. 2) Input - Enter a
-     * Number: 7 Output - It is not an Automorphic Number.
+     * 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 void main(String args[]) throws IOException {
-        BufferedReader br = new BufferedReader(
-            new InputStreamReader(System.in)
-        );
-        System.out.println("Enter a Number: ");
-        int n = Integer.parseInt(br.readLine());
-        if (isAutomorphic(n)) {
-            System.out.println("It is an Automorphic Number.");
-        } else {
-            System.out.println("It is not an Automorphic Number.");
-        }
+    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));
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java b/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java
index 5ec7755a..6bbd1c40 100644
--- a/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java
+++ b/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java
@@ -1,16 +1,25 @@
 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;
 
 public class AutomorphicNumberTest {
 
     @Test
     void testAutomorphicNumber() {
-        assertThat(AutomorphicNumber.isAutomorphic(625)).isTrue();
-        assertThat(AutomorphicNumber.isAutomorphic(144)).isFalse();
-        assertThat(AutomorphicNumber.isAutomorphic(9376)).isTrue();
-        assertThat(AutomorphicNumber.isAutomorphic(169)).isFalse();
+        int trueTestCases[] = { 0, 1, 25, 625, 12890625};
+        int falseTestCases[] = { -5, 2, 26, 1234 };
+        for (Integer n : trueTestCases) {
+            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
     }
 }