-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.py
More file actions
37 lines (25 loc) · 853 Bytes
/
Copy path35.py
File metadata and controls
37 lines (25 loc) · 853 Bytes
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
"""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?"""
def main():
print(solve(1000000))
def solve(number):
primes = list(gen_primes(number))
return len([p for p in primes
if all(map(lambda x: x in primes, circular_permutations(p)))])
def circular_permutations(number):
return [int(str(number)[n:] + str(number)[:n])
for n in range(len(str(number)))]
def gen_primes(max):
p = 2
yield p
primes = [p]
while p < max:
p += 1
if 0 not in (p % x for x in primes):
primes.append(p)
yield p
if __name__ == "__main__":
main()