Note that there are some explanatory texts on larger screens.

plurals
  1. POPython- Prime summands to an even number
    primarykey
    data
    text
    <p>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. </p> <p>Even numbers and primes.</p> <p>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, ...</p> <p>Is this always the case? Can every even number be written as the sum of two primes?</p> <ol> <li>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 &lt; d &lt; 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).</li> <li>Write a prime_sum(n) function. It should accept an even number n>1 as input, and via a loop search for primes p &amp; 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!</li> <li>Main. <ul> <li>Ask the user for an even number n. Continually ask them until they do give you a positive even number.</li> <li>Search for the summands p &amp; q, and either print them out (if they exist) or say they don't. </li> <li>Ask the user if they wish to try with another even, and let them continue until they quit.</li> </ul></li> </ol> <hr> <p>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. </p> <pre><code>def is_prime(n): d=2 while n&gt;1 and d&lt;n**0.5: if n%2==0: c=False d+=1 return c def prime_sum(n): p=3 while n&gt;1: q=n-p if q&lt;=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. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload