Callie
Callie

Reputation: 31

Python- Prime summands to an even number

This is the assignment my professor gave me. I have no idea where to start or what to do! The point is to use loops to figure this out and I can do the loops, but this is blowing my mind.

Even numbers and primes.

A prime number is one that has 1 and itself as its only divisors. 2, 3, 5, 7 and 11 are the first several. Notice that 'being prime' is purely a multiplicative condition -- it has nothing to do with addition. So it might be surprising that if we start listing even numbers, they seem to be the sum (addition!) of two primes. 4 = 2 + 2, 6 = 3 + 3, 8 = 5 + 3, 10 = 3 + 7, 12 = 7 + 5, 14 = 7 + 7, 16 = 13 + 3, ...

Is this always the case? Can every even number be written as the sum of two primes?

  1. Write a is_prime(n) function. It should accept a positive integer n>1 as input, and output True or False, depending on whether n is or is not a prime number. Do this with a loop that checks whether for any integer d, 1 < d < sqrt(n), d divides n. I'd suggest a while loop -- think carefully about the conditional for the loop, and when you want to change this conditional inside the loop. (Use a boolean for your condition).
  2. Write a prime_sum(n) function. It should accept an even number n>1 as input, and via a loop search for primes p & q with p + q = n. Hint: start with p = 3. If (p) and (n-p) are prime you are done. If not, set p+=2 and try again. Make sure you do not search forever!
  3. Main.
    • Ask the user for an even number n. Continually ask them until they do give you a positive even number.
    • Search for the summands p & q, and either print them out (if they exist) or say they don't.
    • Ask the user if they wish to try with another even, and let them continue until they quit.

I didn't know I could edit this! :) So this is what I have so far. I have not tested it yet to debug it b/c I want to get it all down and when the errors pop up I will address them, but if you see any immediate problems let me know.

def is_prime(n):
    d=2
    while n>1 and d<n**0.5:
        if n%2==0:
            c=False
        d+=1
    return c

def prime_sum(n):
    p=3
    while n>1:
        q=n-p
        if q<=0:
            p+=2
            q=n-p
            is_prime(q)
        else:
            is_prime(q)
            is_prime(p)
    while True:
        print("The prime summands of", n, "are", p, "and", q)
    while False:
        print("There are no prime summands of", n)

def main():
    n=eval(input("Gimme an even number please: "))
    while True:
        n=eval(input("That is not a positive even number. Try again: "))
    #not sure how to combine yet, but I am still finishing.
    #will edit again when I have it all down.

Upvotes: 2

Views: 5221

Answers (2)

dting
dting

Reputation: 39287

Prime Number

A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself.

A)

def is_prime(n):                 # Write a is_prime(n) function.
    if n <= 1:                   # It should accept a positive integer n>1 
        return False
    if n == 2:                   # 2 has 2 divisors 1 and itself satisfying definition
        return True
    i = 2                        # Start from 2 and check each number to the sqrt(n)
    while i < n**0.5:            # sqrt(n) can be written as n**0.5
        if n % i == 0:           # If n is divisible by i, which is not 1 or itself, 
            return False         #    return False (not prime)
        i+=1                     # Increment i by 1 and check looping condition
    return True                  # If loop breaks, return True (prime)

Primes can be discovered in a variety of ways. This is one of the most basic with the only optimisation being that the divisor to check is stopped at the root of n instead of checking every number to n.

The most basic probably being:

def is_prime(n):
    if n < 2:
        return False
    for i in range(2,n):
        if n % i == 0:
            return False
    return True

B)

def prime_sum(n):
    if n % 2 or n < 1:                          # if n is odd or less than 1 return invalid
        return "invalid input"
    p = 3
    while n-p > 0:
        if is_prime(p) and is_prime(n-p):       
            return (p, n-p)                     # if both conditions are met, return prime tuple
        p+=2                                    # only check odd numbers
    return "no answer found"                    

Upvotes: -1

joelt
joelt

Reputation: 2680

Don't worry about the big picture of the assignment being difficult. Just go step by step as the prof has broken it down.

Upvotes: 1

Related Questions