Created
November 7, 2008 22:40
-
-
Save terrbear/22985 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def problem37 | |
#The number 3797 has an interesting property. Being prime itself, it is possible to | |
#continuously remove digits from left to right, and remain prime at each stage: | |
#3797, 797, 97, and 7. | |
#Similarly we can work from right to left: #3797, 379, 37, and 3. | |
#primes = [37, 797, 3797] | |
primes = [] | |
lefts = [2,3,5,7] | |
rights = [3,7] | |
middles = [1,3,7,9] | |
lefts.each do |left| | |
rights.each do |right| | |
num = (left.to_s + right.to_s).to_i | |
primes << num if is_prime_right(num) && is_prime_left(num) | |
middles.each do |middle| | |
num = (left.to_s + middle.to_s + right.to_s).to_i | |
primes << num if is_prime_right(num) && is_prime_left(num) | |
middles.each do |middle2| | |
num = (left.to_s + middle.to_s + middle2.to_s + right.to_s).to_i | |
primes << num if is_prime_right(num) && is_prime_left(num) | |
middles.each do |middle3| | |
num = (left.to_s + middle.to_s + middle2.to_s + middle3.to_s + right.to_s).to_i | |
primes << num if is_prime_right(num) && is_prime_left(num) | |
middles.each do |middle4| | |
num = (left.to_s + middle.to_s + middle2.to_s + middle3.to_s + middle4.to_s + right.to_s).to_i | |
primes << num if is_prime_right(num) && is_prime_left(num) | |
end | |
end | |
end | |
end | |
end | |
end | |
puts "primes: #{primes.inspect} (#{primes.size})" | |
puts "sum: #{primes.sum}" | |
#so the numbers so far are 3797, 379, 797, 97, 37 (only 6 more to find) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment