-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp35.py
More file actions
59 lines (42 loc) · 1.61 KB
/
p35.py
File metadata and controls
59 lines (42 loc) · 1.61 KB
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
"""
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 time # because i wanted to time the algorithm
def CircularPrimesSOE(n):
primes = [True for i in range(n+1)]
p = 2
while p * p <= n:
if primes[p] == True:
for i in range(p**2, n+1, p):
primes[i] = False
p += 1
primes[0] = False
primes[1] = False
fprimes = []
for p in range(n+1):
if primes[p]:
fprimes.append(p)
for prime in fprimes:
variations = []
loopednum = str(prime)
for i in range(len(loopednum)):
loopednum += loopednum[0]
loopednum = loopednum[1:]
variations.append(int(loopednum))
for variation in variations:
if variation in fprimes:
continue
else:
fprimes.pop(fprimes.index(prime))
break
return fprimes
if __name__ == "__main__":
print("This code will take circa 3 minutes to run, if you want faster then don't fucking run it in python you nonce. Learn C++ headass")
start_time = time.time()
n = 1000000
primes = CircularPrimesSOE(n)
print(f"Following are the Circular prime numbers smaller than or equal to {n}:\n",primes)
print(f"There are {len(primes)} circular primes below 1 million")
print("--- %s seconds ---" % (time.time()-start_time))