35 done, slow but it works
This commit is contained in:
58
35/main.py
Normal file
58
35/main.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
'''
|
||||||
|
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
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def sieve(n):
|
||||||
|
assert n > 1
|
||||||
|
|
||||||
|
ns = [True] * n
|
||||||
|
|
||||||
|
for i in range(2, math.ceil(np.sqrt(n))):
|
||||||
|
if ns[i]:
|
||||||
|
j = pow(i, 2)
|
||||||
|
|
||||||
|
while j < n:
|
||||||
|
ns[j] = False
|
||||||
|
j = j + i
|
||||||
|
|
||||||
|
return [i for i,val in enumerate(ns) if val][2:]
|
||||||
|
|
||||||
|
def rotate(n):
|
||||||
|
if n < 10:
|
||||||
|
return n
|
||||||
|
else:
|
||||||
|
l = n % 10
|
||||||
|
return l * 10**(len(str(n)) - 1) + n // 10
|
||||||
|
|
||||||
|
def isCircular(p, primes):
|
||||||
|
result = True
|
||||||
|
|
||||||
|
cur = rotate(p)
|
||||||
|
while cur != p:
|
||||||
|
if cur not in primes:
|
||||||
|
result = False
|
||||||
|
break
|
||||||
|
cur = rotate(cur)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Hello this is Patrick")
|
||||||
|
|
||||||
|
primes = sieve(1000000)
|
||||||
|
res = 0
|
||||||
|
|
||||||
|
for p in primes:
|
||||||
|
if isCircular(p, primes):
|
||||||
|
print(p)
|
||||||
|
res += 1
|
||||||
|
print("Number of circular primes:", res)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user