Tuesday 8 January 2019

Rams Scala Puzzles: Automorphic Number

What is Automorphic Number?

A number is called Automorphic number if and only if its square ends in the same digits as the number itself.

For instance,

1. 25 is an "Automorphic" number because 25*25 = 625 (which ends with 25)
2. 12 is not an "Automorphic" number because 12*12 = 144 (which does not end with 12)

Now its good to start solve this problem in Scala language.

Solution in Scala:

object AutomorphicNumberApp extends App {
def automorphicNumber(num: Int): String = {
 if((num*num+"").endsWith(""+num)) "Automorphic Number"
 else "Not Automorphic Number"
}
}

Test:

Import this into Scala REPL and test it as follows:

scala> automorphicNumber(25)
res54: String = Automorphic Number

scala> automorphicNumber(5)
res55: String = Automorphic Number

scala> automorphicNumber(13)
res56: String = Not Automorphic Number

scala> automorphicNumber(12)
res57: String = Not Automorphic Number

scala> automorphicNumber(2)
res58: String = Not Automorphic Number

scala> automorphicNumber(1)
res59: String = Automorphic Number

scala> automorphicNumber(15)
res60: String = Not Automorphic Number

Thank you for reading my tutorials

No comments:

Post a Comment