-
Notifications
You must be signed in to change notification settings - Fork 6
/
problem-035.rb
70 lines (55 loc) · 1.18 KB
/
problem-035.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby
# How many circular primes are there below one million?
# See if a number is prime
def prime?(number)
if number <= 1
return false
end
(2..Math.sqrt(number)).each do |n|
if number%n == 0
return false
end
end
return true
end
# Get circular rotations of the number, excluding the number
def circular_rotations(number)
rotations = []
if number.to_s.size == 1
return rotations
end
digits = number.to_s.split("").map { |x| x.to_i }
(number.to_s.size-1).times {
digits << digits.shift
rotations << digits.to_s.to_i
}
return rotations
end
=begin
Check to see if a number is a circular prime
Function assumes that the number is already a prime and checks to
see if the number's rotations are prime
Returns false when it finds the first non-prime rotation
=end
def circular_prime?(number)
if number.to_s.size == 1
return true
end
rotations = circular_rotations(number)
rotations.each do |r|
if !prime?(r)
return false
end
end
return true
end
###
count = 0
(1..1000000).each do |number|
if prime?(number)
if circular_prime?(number)
count += 1
end
end
end
puts "Answer: #{count}"