import Foundation
/*
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
{
func CheckAutomorphicNumber(num: Int) -> Bool {
let s = String(num)
let square = num * num
let last = square % Int(pow(10.0, Double(s.count)))
print(num, "square =", square, terminator:" ")
return last == num
}
static func main(_ args: [String])
{
let obj: Automorphic? = Automorphic()
let n = 25 // 625
print(obj!.CheckAutomorphicNumber(num: n))
print(obj!.CheckAutomorphicNumber(num: 5))
print(obj!.CheckAutomorphicNumber(num: 76))
print(obj!.CheckAutomorphicNumber(num: 98))
print(obj!.CheckAutomorphicNumber(num: 376))
}
}
Automorphic.main([String]())
/*
run:
25 square = 625 true
5 square = 25 true
76 square = 5776 true
98 square = 9604 false
376 square = 141376 true
*/