Created
January 24, 2019 10:38
-
-
Save shankar524/f13dacfa644edf30a18e9855aea88d30 to your computer and use it in GitHub Desktop.
Curious Number A n-digit number is said to be curious if the last n digits of its square are the same as the original number. For example, 25^2 = 625 and 76^2 = 5776. (Curious numbers are also known as automorphic numbers.) An example of 9 digit number: 212890625^2 = 45322418212890625 Write a program to find all Curious Numbers of 1 to 10 digits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def square(number=0) | |
number ** 2 | |
end | |
def is_curious_number(number) | |
diff = square(number) - number | |
last_digits=diff % (10**(number.digits.count)) | |
if(last_digits==0) | |
return true | |
else | |
return false | |
end | |
end | |
#checking | |
puts is_curious_number(25) #=>true | |
puts is_curious_number(26) #=>false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment