Last active
August 29, 2015 14:00
-
-
Save tnedev/11233573 to your computer and use it in GitHub Desktop.
Goldbach Conjecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Implement a function, called goldbach(n) which returns a list of tupples, that is the goldbach conjecture for the given number n | |
The Goldbach Conjecture states: | |
Every even integer greater than 2 can be expressed as the sum of two primes. | |
Keep in mind that there can be more than one combination of two primes, that sums up to the given number. | |
The result should be sorted by the first item in the tuple. | |
For example: | |
4 = 2 + 2 | |
6 = 3 + 3 | |
8 = 3 + 5 | |
10 = 3 + 7 = 5 + 5 | |
100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53 | |
""" | |
def goldbach(n): | |
counter = 1 | |
counter2 = 0 | |
firstValue = [] | |
secondValue = [] | |
while counter <= n: | |
if IsPrime(counter)==True: | |
counter2 = counter | |
while counter2<=n: | |
if IsPrime(counter2)==True and counter+counter2==n: | |
firstValue.append(counter) | |
secondValue.append(counter2) | |
counter2+=1 | |
counter+=1 | |
return list(zip(firstValue, secondValue)) | |
def IsPrime(n): | |
if n == 1: | |
return False | |
elif n == 2: | |
return True | |
for x in range (2, n): | |
if n%x == 0: | |
return False | |
else: | |
return True | |
inputNum=0 | |
while inputNum%2!=0 or inputNum<2: | |
inputNum = input ("Choose an even number: ") | |
inputNum = int(inputNum) | |
print(goldbach(inputNum)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment