From e37a0d889896c38134840f352f63115e57b27615 Mon Sep 17 00:00:00 2001 From: Taranjeet Singh Kalsi Date: Sat, 29 Oct 2022 18:39:11 +0530 Subject: [PATCH 1/2] Added functions and refactored code --- .../maths/AutomorphicNumber.java | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java b/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java index 61bdd32f8beb..3ea898cc4a7f 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)); } } From 1f0bbac7450da42d16f9ccfa60c5f727b925e93e Mon Sep 17 00:00:00 2001 From: Taranjeet Singh Kalsi Date: Sat, 29 Oct 2022 18:57:46 +0530 Subject: [PATCH 2/2] Added Test cases for new functions --- .../maths/AutomorphicNumberTest.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java b/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java index 5ec7755ac35d..6bbd1c4059cf 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 } }