Skip to content

Instantly share code, notes, and snippets.

@MiSawa
Created October 22, 2012 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiSawa/3931689 to your computer and use it in GitHub Desktop.
Save MiSawa/3931689 to your computer and use it in GitHub Desktop.
PE problem 0092
#!/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