How to check if a number is automorphic number in TypeScript

1 Answer

0 votes
/*
    An Automorphic number is a number whose square ends with the same digits 
    as the original number. E.g – 5 : 5 * 5 = 25 //ends with 5 
 
*/
class Automorphic
{
	public CheckAutomorphicNumber(num: number) {
        const s = num.toString();  
        
        const square = num * num;
        
        const last = square % Math.pow(10, s.length);
        
        console.log(num + " square = " + square);
        
        return last == num;
    }

	public static main(args: string[])
	{
		var obj = new Automorphic();
		
        const n = 25; // 625
    
        console.log(obj.CheckAutomorphicNumber(n));
        console.log(obj.CheckAutomorphicNumber(5));
        console.log(obj.CheckAutomorphicNumber(76));
        console.log(obj.CheckAutomorphicNumber(98));
        console.log(obj.CheckAutomorphicNumber(376));
	}
}
Automorphic.main([]);




/*

run:

"25 square = 625" 
true 
"5 square = 25" 
true 
"76 square = 5776" 
true 
"98 square = 9604" 
false 
"376 square = 141376" 
true 

*/
  

 



answered Oct 20, 2021 by avibootz
edited Oct 21, 2021 by avibootz

Related questions

...