|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * Wikipedia link for Automorphic Number : https://en.wikipedia.org/wiki/Automorphic_number |
4 | 5 | * A number is said to be an Automorphic, if it is present in the last digit(s)
|
5 | 6 | * of its square. Example- Let the number be 25, its square is 625. Since,
|
6 | 7 | * 25(The input number) is present in the last two digits of its square(625), it
|
7 | 8 | * is an Automorphic Number.
|
8 | 9 | */
|
9 |
| -import java.io.*; |
| 10 | + |
| 11 | +import java.math.BigInteger; |
10 | 12 |
|
11 | 13 | public class AutomorphicNumber {
|
12 | 14 |
|
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) |
36 | 24 | 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; |
37 | 30 | }
|
| 31 | + long lastDigits = square % (long) Math.pow(10, numberOfdigits); // Extracting last Digits of square |
| 32 | + return n == lastDigits; |
38 | 33 | }
|
39 | 34 |
|
40 | 35 | /**
|
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} |
44 | 41 | */
|
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)); |
56 | 62 | }
|
57 | 63 | }
|
0 commit comments