-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp035.py
83 lines (70 loc) · 2.24 KB
/
p035.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# Project Euler
# Problem 35
# http://projecteuler.net/problem=35
# The number, 197, is called a circular prime because all rotations of
# the digits: 197, 971, and 719, are themselves prime.
# How many circular primes are there below one million?
from __future__ import print_function
import math
def sieve(n,primes=[2]):
for i in xrange(primes[len(primes)-1],int(n)):
if( i&1==0 ): continue
if( i%5==0 and i!=5 ): continue
ifprime = True
for j in primes:
if i%j == 0:
ifprime = False
break
if ifprime:
if( not i in primes ): primes.append(i)
if (i-1)%10000==0: print(i)
return primes
def isPrime(n,primes=[2]):
if(n&1==0 or n%5==0): return (n==2 or n==5)
for i in primes:
if(n%i==0): return False
for i in xrange(primes[len(primes)-1],int(math.sqrt(n))+1):
ifprime = True
for j in primes:
if i%j == 0:
ifprime = False
break
if ifprime:
if n%i==0: return False
if( not i in primes ): primes.append(i)
#print primes
for i in primes:
if n%i == 0:
return False
return True
def getRotations(n):
n = str(n)
rots = []
for j in xrange(len(n)):
rotation = ""
for i in xrange(j,len(n)+j):
rotation += n[i%len(n)]
rots.append(rotation)
return rots
if __name__ == "__main__":
primes = [2]
for num in xrange(2,10**6):
if(isPrime(num)): primes.append(num)
print("Finished finding primes. Now looking for circular primes.")
circPrimes = {}
for i in primes:
if( i in circPrimes ): continue
rotations = getRotations(i)
print(rotations)
circular = True
if( '2' in str(i) or '4' in str(i) or '6' in str(i) or '8' in str(i) or '0' in str(i) or '5' in str(i) ): circular = (i==2 or i==5)
for j in rotations:
if not circular: break
if( not int(j) in primes ): circular = False; break
for j in rotations: circPrimes[j]=circular
if (i-1)%10000==0: print(i-1)
answer = 0
for i in circPrimes.itervalues():
if i: answer += 1
print(answer)