Last active
March 25, 2018 09:18
-
-
Save hyuki/90b40e37cb6a5a2d049a78db8d1d2376 to your computer and use it in GitHub Desktop.
Squared digit sum. See https://twitter.com/AlgebraFact/status/977543329046056961 and https://www.johndcook.com/blog/2018/03/24/squared-digit-sum/
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 sqsum(x) | |
y = 0 | |
x.to_s.split(//).each do |s| | |
a = s.to_i | |
y += a ** 2 | |
end | |
y | |
end | |
def seq_sqsum(x) | |
h = {} | |
while not h[x] | |
print "#{x} -> " | |
h[x] = true | |
x = sqsum(x) | |
end | |
puts "#{x} -> ..." | |
end | |
seq_sqsum(4) | |
seq_sqsum(1000) | |
seq_sqsum(3) | |
seq_sqsum(31) | |
seq_sqsum(314) | |
seq_sqsum(3141) | |
seq_sqsum(31415) | |
seq_sqsum(314159) | |
seq_sqsum(3141592) | |
seq_sqsum(31415926) | |
seq_sqsum(314159265) |
Author
hyuki
commented
Mar 25, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment