Skip to content

Instantly share code, notes, and snippets.

@ChrisLundquist
Last active June 10, 2018 01:01
Show Gist options
  • Save ChrisLundquist/a0f330486c25f11ba1d0acdfdb8c26cb to your computer and use it in GitHub Desktop.
Save ChrisLundquist/a0f330486c25f11ba1d0acdfdb8c26cb to your computer and use it in GitHub Desktop.
euler 35.rb
#!/usr/bin/env ruby
# The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
# There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
#How many circular primes are there below one million?
#
require 'prime'
candidates = [2] + Prime.each(1_000_000).reject { |p| p.digits.any? { |d| d.even? } }
def siblings(int)
a = int.digits.reverse
Array.new(a.size).map { |i| a.unshift(a.pop).join.to_i }
end
candidates = candidates.select { |c| siblings(c).all? { |s| s.prime? } }
puts candidates.inspect
puts candidates.count
puts candidates.all? { |c| c.prime? }
[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 197, 199, 311, 337, 373, 719, 733, 919, 971, 991, 1193, 1931, 3119, 3779, 7793, 7937, 9311, 9377, 11939, 19391, 19937, 37199, 39119, 71993, 91193, 93719, 93911, 99371, 193939, 199933, 319993, 331999, 391939, 393919, 919393, 933199, 939193, 939391, 993319, 999331]
55
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment