Created
October 22, 2012 14:12
-
-
Save MiSawa/3931689 to your computer and use it in GitHub Desktop.
PE problem 0092
This file contains hidden or 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
#!/usr/bin/ruby | |
def f(n) | |
n.to_s.split('').map(&:to_i).inject(0){|a,b|a+b**2} | |
end | |
a = Hash.new do |hash, n| | |
hash[n] = (n == 1 || n == 89) ? n : hash[f(n)] | |
end | |
p 1.upto(1E7).count{|n|a[f(n)] == 89} | |
#=> 8581146 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment