I am trying to complete an assignment for my beginner's computer science. I have to write a program that proves that every even number greater than four is the sum of two prime numbers. I prompt the user for the top end of the range (the lowest being four) and then prove Goldbach's theory. However, I have to determine which numbers of the factoring are prime in another function. Here is what I have so far:

import sys

def main():
    print "This program will verify Goldbach's Conjecture from 4 to a number of your choosing."
    num = input("What should the top end of the range be? ")
    findPrimeAddends(num)

def isPrime(n):
    for n in range(4,int(num**0.5)+1):
        if(num % n == 0):
            return False
        return True,0
            
def findPrimeAddends(num):
    for n in range(4, num + 1): 
        x = n/2
        (a,b) = isPrime(n)
        if(a > b): 
            sys.stdout.write("%d = %d + %d\n" % (x,a,b))       
        
main()

Right now, I keep getting an error because I haven't defined "num" in the isPrime function. Can anyone help me out?

First find the summing numbers. You have no "num" because you have no sums. For the number 8 for example, 4+4 or 6+2 will not work, so it has to be 5+3 or 7+1. To test the number 8, you would have to test 1, 2, 3, 4, 5, 6, 7 (1+7, 2+6, 3+5, 4+4), and that would be the case with most even numbers, so you would test 1 & number-1, 2 and number-2, etc. I would suggest that you first create a set of prime numbers up to whatever point you are supposed to go and then lookup the number in the set of primes, instead of testing the same numbers for prime every time through.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.