Goldbachs Conjecture Explained and Python Implementation

Conjecture states that Every even integer greater than 2 can be written as the sum of two primes’,

A Goldbach number is a positive number which can be written  the sum of two odd primes and these two primes are called as Goldbach partition.

For small values of n, the strong Goldbach conjecture (and hence the weak Goldbach conjecture) can be verified directly. For instance, Nils Pipping in 1938 laboriously verified the conjecture up ton ≤ 105. With the advent of computers, many more values of n have been checked; T. Oliveira e Silva is running a distributed computer search that has verified the conjecture for n ≤ 4 × 1018 (and double-checked up to 4 × 1017). One record from this search is that 3325581707333960528 is the smallest number that has no Goldbach partition with a prime below 9781.(Source:Wikipedia)

The following goldbach function takes the positive number and returns the two prime

from math import *

def goldbach(number):
    x, y = 0, 0
    result = 0
    if not number % 2:
        prime_list = primeslist(number)
        while result != number:
            for i in range(len(prime_list)):
                x = prime_list[i]
                if result == number: break
                for j in range(len(prime_list)):
                    y = prime_list[j]
                    result = x + y
                    
                    if result == number:  
                       return x, y 

Following function decides whether the following number is prime or not.

def is_prime(number):
    if number % 2:
        # equivalent to if number % 2 != 0 because if number is
        # divisible by 2 it will return 0, evaluating as 'False'.
        for num in range(3, int(sqrt(number)) + 1, 2):
            if number % num == 0:
               return False
        return True
    else:
        return False

def primeslist(number):
    prime_list = []
    for x in range(2, number + 1):
            if is_prime(x):
                prime_list.append(x)
    return prime_list



def main():
    while True:
        usr_in = (input("Please enter a positive number greater than 1: "))
        if usr_in > 1:
            break
        else:
            print("Number not valid.")

    prime_list = goldbach(usr_in)
    print(prime_list)
    # prime_list = list_of_primes(usr_in)
    # for x in prime_list:
    #     print(x)


if __name__ == '__main__':
    main()

2 thoughts on “Goldbachs Conjecture Explained and Python Implementation

  1. hello here is my proof for goldbachs conjecture
    case1 E=p1+p2
    case 2 E= p1+c1 (e= even,p=prime,c= composite)
    but c1=p3+p4+p5 ( proven)
    therefore E=p1+p3+p4+p5
    Now any combination of 2 of p1+p3+p4+p5 will give you an even number.
    When these are moved to left side as E-p1-p3=p4+p5
    E-p1-p3=even number E2
    E2=p4+p5 proving Goldbach strong conjecture

    Like

Leave a Reply Here