Skip to content

Added functions in AutomorphicNumber.java #3735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 44 additions & 38 deletions src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}