Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Goldbach's other conjecture : Problem 46 Pyhton Code

Wednesday, October 15, 2014

Goldbach's other conjecture : Problem 46 Pyhton Code

Goldbach's other conjecture : Problem 46

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
9 = 7 + 2×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12
It turns out that the conjecture was false.
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

Problem Source :  Euler Project

Python Code

import math
def goldbachConjecture():
    found = False
    def checkPrime(numb):
        prime = True
        if numb == 1:
            return False
        for i in range(int(math.sqrt(numb))):
            i = i+2
            if numb%i == 0:
                prime = False
                break
        if numb == 2:
            return True
        else:           
            return prime
    def nextOddCompositeNumber(numb):
        numb = numb+1
        while checkPrime(numb) == True or numb %2 == 0:
            numb = numb+1
        return numb;           
    numb = 9    
    while found == False:
        k = 1
        goldtrue = False
        while numb-2*k**2 > 0 :
            if checkPrime(numb-2*k**2) == True:
                goldtrue = True
                break
            k = k+1   
        if goldtrue == True:
            found = False
            numb = nextOddCompositeNumber(numb)
        else:
            found = True
            print "smallest odd composite that cannot be written as the sum of a prime and twice a square",numb
goldbachConjecture()           

No comments:

Post a Comment