Check Automorphic Number

An automorphic number is that number whose square ends in the same digits as the number. such as

5 sqr = 25 ends with 5

6 sqr = 36 ends with 6

76 sqr = 5776 ends with 76

376 sqr = 141376 ends with 376

package org.wesome.dsalgo;

public class Automorphism {

    public static int checkAutomorphism(int number) {
        int digitCount = 0, square = number * number;
        int digits = number;
        /*  find digit count of number  */
        while (digits > 0) {
            digitCount++;
            digits = digits / 10;
        }
        /*  */
        int power = (int) Math.pow(10, digitCount);
        int lastSquareDigits = square % (power);
        if (number == lastSquareDigits) {
            return square;
        } else {
            return -1;
        }
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.Automorphism.checkAutomorphism;

public class CheckAutomorphismTest {
    @Test
    void checkAutomorphismTest1() {
        int number = 5;
        Assertions.assertEquals(25, checkAutomorphism(number));
    }

    @Test
    void checkAutomorphismTest2() {
        int number = 376;
        Assertions.assertEquals(141376, checkAutomorphism(number));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on