Skip to content

Happy Pi Day

by cory on March 14th, 2008
Today is 3/14 (if you're using the US way of writing dates), aka Pi Day! A few years ago, Google created a really clever job posting (more here). They put up banners and bought billboards in areas of the country with lots of smart mathematicians, such as Harvard Square in Boston, that said: "(first 10-digit prime in consecutive digits of pi).com". If you calculate that 10-digit number and combine it with ".com" to create a domain name, the website that comes up challenges you to another puzzle. Eventually, after answering several challenges correctly, you're directed to a page that invites you to apply to work at Google. Pretty clever. I found out about it a few years too late, but took the challenge upon myself anyway and wrote a perl script that calculated the correct answer. On this Pi Day I am endeavoring to code up a solution again, using my new favorite programming language, Ruby, mostly just for fun but also to see if my abilities have improved any since the last time. Unfortunately, the blog post I wrote about my perl solution has somehow been lost to the winds of /dev/null, so I can't compare the code outright. But I will say that this time it only took me about 10 minutes to code up this solution:
#!/opt/local/bin/ruby

module Prime
  def prime?
    return false if self % 2 == 0 # divisible by 2

    # check odd divisors from 3 to the square root of this number
    (3..(Math.sqrt(self))).step(2) {|i| return false if self % i == 0}

    # didn't find any divisors
    return true
  end
end

# mix the module into the bignum and fixnum classes
class Bignum
  include Prime
end
class Fixnum
  include Prime
end

# digits of pi from http://www.eveandersson.com/pi/digits/
pi_digits_filename = "10000.txt" 

pi_digits = File.open( pi_digits_filename ).read
pi_digits.gsub!("\n","") # strip newlines
pi_digits.gsub!(".","") # remove the "." in 3.14...

# iterate 10-character strings
0.upto(pi_digits.length - 10) do |i|
  num = pi_digits[i,10].to_i
  if num.prime?
    puts num # found it
    break
  end
end
Update: Ok, it appears that the Google challenge was to find that prime in consecutive digits of e, not pi. Oh well. Next year I'll do this exercise with whatever language is hot on 2/18.

From → ruby

2 Comments
  1. This guy made a dynamic CGI directory listing in apache and gives you a link to the next few digits of pi (maybe primes). I thought this was pretty cool. It breaks at 4096 digits (browser POST limit) but works at 4095 digits (including any slashes you want to throw in there).

    http://nonsensicality.com/3/

    I blogged a bit about it here:
    http://ubernub.com/2010/02/05/pi-not-pie-cgi

  2. This guy made a dynamic CGI directory listing in apache and gives you a link to the next few digits of pi (maybe primes). I thought this was pretty cool. It breaks at 4096 digits (browser POST limit) but works at 4095 digits (including any slashes you want to throw in there).

    http://nonsensicality.com/3/

    I blogged a bit about it here:

    http://ubernub.com/2010/02/05/...

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS