Skip to content

Instantly share code, notes, and snippets.

@bragboy
Created November 12, 2015 10:55
Show Gist options
  • Select an option

  • Save bragboy/7529cf5a2014dbd638b1 to your computer and use it in GitHub Desktop.

Select an option

Save bragboy/7529cf5a2014dbd638b1 to your computer and use it in GitHub Desktop.
def find_prime(n)
return false if n <= 1
2.upto(Math.sqrt(n).to_i) do |x|
return false if n%x == 0
end
true
end
large = []
1.upto(1000000) { |each_num| large[each_num] = true if find_prime(each_num) }
count = 0
1.upto(1000000) do |num|
str = num.to_s
all = (0...str.length).collect { |i| large[((str * 2)[i, str.length]).to_i] ? true : false }
count+=1 if all.inject(0){|b,i| b&&=i}
end
puts "The count of such primes is #{count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment