Skip to content

Instantly share code, notes, and snippets.

@andersonvom
Created April 13, 2012 22:36
Show Gist options
  • Save andersonvom/2380578 to your computer and use it in GitHub Desktop.
Save andersonvom/2380578 to your computer and use it in GitHub Desktop.
All 7-digit prime palindromes in the first 100.000 digits of PI
class Email
def self.arccot(x, unity)
xpow = unity / x
n = 1
sign = 1
sum = 0
loop do
term = xpow / n
break if term == 0
sum += sign * (xpow/n)
xpow /= x*x
n += 2
sign = -sign
end
sum
end
def self.calc_pi(digits = 10000)
fudge = 10
unity = 10**(digits+fudge)
pi = 4*(4*arccot(5, unity) - arccot(239, unity))
pi / (10**fudge)
end
def self.prime?(n)
return false if n%2 == 0
i = 3
while ( i <= Math.sqrt(n)) do
return false if (n%i) == 0
i += 2
end
true
end
def self.palindrome?(str)
str == str.reverse
end
def self.find_email(digits)
pi = calc_pi(digits).to_s
(digits-7).times do |i|
str = pi[0+i, 7]
puts "position=#{i} | str=#{str}" if self.palindrome?(str) and self.prime?(str.to_i)
end
end
end
@davidmachadosf
Copy link

Why did you named you class "Email" ????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment