#!/usr/bin/ruby # solution of http://http://abstrusegoose.com/126 # Author: Yoann Guillot # License: WTFPLv2 class Integer def prime? return false if self % 2 == 0 or self == 1 (3..(Math.sqrt(self))).step(2) { |i| return false if self % i == 0 } true end end def firstprime(num, primelen) (num.length-primelen).times { |i| subnum = num[i, primelen].to_i return subnum if subnum.prime? } end def largestrighttruncatableprime pool = [0] nextgen = proc { ng = [] pool.each { |p| (0..9).each { |e| ng << (p*10+e) if (p*10+e).prime? } } pool = ng if not ng.empty? # implicit return true } nil while nextgen[] pool.max end pi, e = DATA.read.split('__END__').map { |d| d.gsub(/[^0-9]/m, '') } # first 10-digits prime found in digits of e puts "a#{firstprime(e, 10)}" # first 10-digits prime found in digits of pi puts "b#{firstprime(pi, 10)}" # first 9-digits prime found in digits of pi # + first 9-digits prime found in digits of e # + largest right-truncatable prime puts "c#{firstprime(pi, 9) + firstprime(e, 9) + largestrighttruncatableprime}" __END__ http://www.eveandersson.com/pi/digits/ pi = 3. 14159265358979323846264338327950288419716939937510 58209749445923078164062862089986280348253421170679 82148086513282306647093844609550582231725359408128 __END__ http://www-groups.dcs.st-and.ac.uk/~history/HistTopics/e_.html e = 2. 718281828459045235360287471352662497757247093699959574966967627724076630353 547594571382178525166427427466391932003059921817413596629043572900334295260