-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoldbach test.py
91 lines (79 loc) · 2.94 KB
/
Goldbach test.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
83
84
85
86
87
88
89
90
91
'''
select even number - 24
generate ODD primes up to even number - 3, 5, 7, 11...
prime[0]: - 3
+ prime[1] - 5
+ prime[2] - 7
(subsequent primes > prime[0])
...
until prime sum > even number or primes exhausted
prime[1]: - 5
+ prime[2] - 7
+ prime[3] - 11
...
...
until prime[x] > even number
for each pair forming the even number, add to list
select next even number - 26
generate more ODD primes - avoid regeneration!
...
'''
from math import sqrt
def dgtlRoot(number):
number = sum([int(char) for char in str(number)])
if number >= 10: return dgtlRoot(number)
return number
def genOddPrimes(entry):
for num in range(1, entry): # From the last even number to the next...
factors = [] # Stores factors, resets with each new number
x = 1 # Incremented
# Digital Root/Final digit NON-PRIME criteria
if (str(num)[-1] in '245680' and num not in [5]) or \
(dgtlRoot(num) % 3 == 0 and num != 3): # [5] should be [2,5], but...
#...we're excluding odd primes
continue # Skips
while x <= sqrt(num): # Square root not reached
if num % 2 == 1 and x % 2 == 0: # if num odd and x is even
pass # ...don't bother
elif num % x == 0: # If a factor
if x not in factors: # If factor not already stored
factors.append(x) # Store
if int(num/x) not in factors: # If corresponding factor not stored
factors.append(int(num/x)) # then store
if len(factors) > 2: # Can no longer be primes
break # Stop current number. Saves time.
x += 1
if len(factors) == 2 and num not in primes:
primes.append(num)
# print(f'Primes leading up to {evenNum}:\n{primes}') #T#
primePairList = []
pairCount = 0
if primes is not [] and entry >= 6:
for elem in range(len(primes)):
index = elem
exceeded = False
while not exceeded:
while index < len(primes):
primeSum = primes[elem] + primes[index]
if primeSum > entry:
exceeded = True
break
elif primeSum == entry:
primePairList.append([primes[elem], primes[index]])
pairCount += 1
index += 1
if not index < len(primes):
exceeded = True
if pairCount != 0:
print(f'No. of prime pairs that form {entry}: {pairCount}')
for pair in primePairList:
print(pair)
print()
if __name__ == '__main__':
primes = []
while True:
entry = int(input('Input even number: '))
if entry % 2 == 0:
genOddPrimes(entry)
else:
print(f'{entry} is not an even number.')