-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheuler035.py
53 lines (44 loc) · 1.27 KB
/
euler035.py
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
"""
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?
"""
import math
def sieveofEratosthenes(n):
nums = list(range(2, n + 1))
for i in range(2, int(math.sqrt(n)) + 1):
if nums[i - 2] != 0:
for j in range(i - 2 + i, n - 2 + 1, i):
nums[j] = 0
return set(x for x in nums if x != 0)
def rotate(n):
num_of_digits = 1
n_new = n
while n_new % 10 != n_new:
num_of_digits += 1
n_new /= 10
i = 1
rotates = {n}
while i < num_of_digits:
right = n % 10
left = n // 10
n_rotated = right * 10 ** (num_of_digits - 1) + left
rotates.add(n_rotated)
n = n_rotated
i += 1
return rotates
if __name__ == "__main__":
# print "This program is being run by itself"
n = int(input())
primes = sieveofEratosthenes(n)
count = 0
res = 0
for p in primes:
if rotate(p) <= primes:
print(rotate(p))
count += 1
res += p
print(res)
else:
print
'I am being imported from another module'