I used my prime generator from Problem 27 for this one. It would have been faster to build the rotation into my generator, but it ran fine without it.
How many circular primes are there below one million?
require 'prime_generator'
primer = Prime_Generator.new 1_000_000
def is_rot_prime? primer, chars
chars.size.times do |i|
chars = Array.new(chars.size) { |i| chars[i - 1] }
return false if !primer.is_prime?(chars.join("").to_i)
end
true
end
count = 0
primer.stack.each do |n|
count += 1 if is_rot_prime? primer, n.to_s.split("")
end
# subtract 1 because "1" doesn't count
puts count - 1
Speaking of the rotation, the ruby array initialize methods and negative indexers make it a cinch to rotate. How cool is this?
chars = Array.new(chars.size) { |i| chars[i - 1] }




