-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35. Circular primes.py
58 lines (49 loc) Β· 1.33 KB
/
35. Circular primes.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
def isPrime(n):
# if int(str(n)[-1]) % 2 == 0:
# return False
# if (sum(map(int, list(str(n))))) % 3 == 0:
# return False
for i in range(3, int(n**(1/2)) + 1, 2):
if n % i == 0:
return False
return True
def cycle(number):
numberString = str(number)
cycleString = numberString[1:] + numberString[:1]
return int(cycleString)
def cyclicNumbers(number):
digits = len(str(number))
cyclicNumbersList = [number]
for _ in range(digits-1):
cyclicNumbersList.append(cycle(number))
number = cyclicNumbersList[-1]
return list(set(cyclicNumbersList))
def allPrimes(aList):
digits = len(str(aList[0]))
for i in aList:
if i not in primes[digits-1].keys() or not primes[digits-1][i]:
return False
return True
def hasEven(number):
evenNumbers = ['0','2','4','6','8']
for i in str(number):
if i in evenNumbers:
return True
return False
primes = [{2:True},{},{},{},{},{}]
for i in range(3, 10**6, 2):
if isPrime(i) and not hasEven(i):
primes[len(str(i))-1][i] = True
print((primes[1]))
#print(allPrimes(cyclicNumbers(primes[1][79].key())))
count = 0
for line in primes:
for i in line.keys():
cyclicList = cyclicNumbers(i)
if allPrimes(cyclicList):
digits = len(str(cyclicList[0]))
count += len(set(cyclicList))
for i in set(cyclicList):
primes[digits-1][i] = False
print(*set(cyclicList))
print(count)