Skip to content

Instantly share code, notes, and snippets.

@MiSawa
MiSawa / gist:3931689
Created October 22, 2012 14:12
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
p 1.upto(9999).count{|n|
50.times.all?{
n += n.to_s.reverse.to_i
n.to_s != n.to_s.reverse
}
}
#=> 249
$facto = [1]
for i in 1..10
$facto[i] = $facto[i-1] * i
end
def f(n)
res = 0
until n.zero?
res += $facto[n%10]
n /= 10
end
require "prime"
mx = 1000
res = [0, 0, 0]
for b in -mx+1...mx
next unless b.prime?
for a in -mx+1...mx
l = 0
l += 1 while (l*l + a*l + b).prime?
res = [res, [l, a, b]].max
require "mathn"
def f(x)
a = []
hash = {}
n = 1/x
while hash[n].nil?
hash[n] = true
n *= 10
n -= n.to_i
end