-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoldbach.py
More file actions
60 lines (46 loc) · 1.47 KB
/
Copy pathgoldbach.py
File metadata and controls
60 lines (46 loc) · 1.47 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
"""Goldbach's Conjecture
Goldbach's conjecture is a rule in math that states the following: every even number greater than 2
can be expressed as the som of two prime numbers.
Write a program that finds every possible pair of prime numbers, whose sum equals the given number
or a set of numbers within a range.
For example:
Input: 16
Output:
3 + 13
5 + 11
Input: 32
Output:
3 + 29
13 + 19
Input: 4, 8
Output:
4: 2 + 2
6: 3 + 3
8: 3 + 5
"""
from itertools import combinations
print("Give a range, start with a number greater than two. Both numbers may be the same")
frm = int(input("From: "))
upto = int(input("Until: "))
def prime(n): # check if the number is a prime
for j in range(2, int(n ** .5) + 1):
if n % j == 0:
return False
return True
def goldbach(getal):
primes = []
# Make array of all primes from 2 until number:
for i in range(2, getal+1):
if prime(i):
primes.append(i)
paren = list(combinations(primes, 2))
for i in range(len(paren)):
som = paren[i][0] + paren[i][1]
if getal % 2 == 0 and som == getal: # check if som of the two pairs is equal with the number
print("{}: {} + {}".format(getal, paren[i][0], paren[i][1]))
for j in range(len(primes)):
som = 2 * primes[j]
if som == getal: # check if two times the prime makes the number
print("{}: {} + {}".format(getal, primes[j], primes[j]))
for k in range(frm, upto+1):
goldbach(k)