The method I have written for finding them is not nearly as sophisticated as those of more skilled mathematicians. It uses a method called the Sieve of Eratosthenes. Basically the program takes a number and tries to divide it by all the numbers below it (except for 1). If any do divide neatly, then it is not a prime number.
Another mathematician called Goldbach came up with a conjecture that any non-prime number could be created by adding two prime numbers together. So for example, 2 is a prime, 4 is not a prime and can be expressed as 2 * 2, or in Goldbach's conjecture 2+2.
Goldbach's conjecture seems to hold true as far as practical calculations show, but nobody is sure if that is because of some important underlying principle or if it is just very easy to do so.
So this program asks for a maximum number. It then goes from 2 to the maximum number, using the sieve of Eratosthenes to find if each number is a prime, and if not then whether it fits Goldbach's conjecture.
And a typical output is:#!/usr/bin/python3def goldbach(evennumber):
for x in primelist:
for y in primelist:
if x + y == evennumber:
print(", Goldbach conjecture: "+str(x)+" + "+str(y)+" = "+str(evennumber))
return()
print("Problem with Goldbach Conjecture!"); exit()count = 0
maxcount = input("Please enter maximum count: ")
try: int(maxcount)
except: print("Bad input"); exit()
else: maxcount = int(maxcount)
testnumber = 2
primelist = [2]
while count < maxcount-2 :
testnumber +=1
prime = True
count += 1
for testfactor in primelist:
if testnumber % testfactor == 0:
print(str(testnumber)+" is divisible by "+str(testfactor), end='')
prime = False
if testfactor == 2: goldbach(testnumber)
else: print('')
break
if prime == True:
primelist.append(testnumber)
print(str(testnumber) + " is a prime!")#print(primelist)
print("Length of prime list is "+str(len(primelist)))
>>> ================================ RESTART ================================
>>>
Please enter maximum count: 20003 is a prime!
4 is divisible by 2, Goldbach conjecture: 2 + 2 = 4
5 is a prime!
6 is divisible by 2, Goldbach conjecture: 3 + 3 = 6
7 is a prime!.....
1996 is divisible by 2, Goldbach conjecture: 3 + 1993 = 1996
1997 is a prime!
1998 is divisible by 2, Goldbach conjecture: 5 + 1993 = 1998
1999 is a prime!
2000 is divisible by 2, Goldbach conjecture: 3 + 1997 = 2000
Length of prime list is 303
>>>
No comments:
Post a Comment