In 1742, a German amateur mathematician named Christian Goldbach wrote to Leonard Euler with the following conjecture:
Every number greater than 2 can be written as the sum of three prime numbers.
In his conjecture, Goldbach was considering 1 as a prime number. By convention we no longer consider 1 as a prime number---and Euler later modified the conjecture to its final form:
Every even number greater than or equal to 4 can be expressed as the sum of two prime numbers. For example:
There is no formal proof of this conjecture. However, we can verify Goldbach's conjecture in certain ranges.
For this assignment, you will write a program that verifies Goldbach's conjecture in the range of 4 to a number entered by the user (inclusive). Your output will print all even numbers in that range in the form of n = a + b, where a and b are prime numbers and a <= b. Each even number should be on a separate line followed by all possible unique pairs of prime numbers.
You may work in pairs for this assignment, but you must follow the guidelines covered in Pair Programming Guidelines. If you work in a pair, you will receive two extra credit points on your grade for this assignment. You must partner with someone in your lecture.
Note: You may not use any programming construct that we have not covered in class. You may use math.sqrt() but you may not use any other function from the math library.
File Name: Goldbach.py
Your program should prompt the user for the top end of the range. Your program should then list each even number within that range along with the unique pairs of prime numbers that sum to it. It should do this using the findPrimeAddends() function.
findPrimeAddends() is a function written by you that accepts one parameter, a number. It should find all pairs of primes that sum to that number and print them in the appropriate format. It should determine which numbers are prime using the isPrime() function.
isPrime() is a function written by you that accepts a single parameter, a number. It determines whether that number is or is not prime, and returns either True or False reflecting whether the number is or is not prime.
You may find sys.stdout.write() useful for this program. We went over how to use it with strings in class, but you can also give it more complicated formats as you would print. For instance:
myString="number" num=12 sys.stdout.write("The %s is %d\n" % (myString, num))would print
The number is 12However, sys.stdout.write() only accepts strings. So, for instance, sys.stdout.write(4) would cause an error.
Here is a sample run of the program with the user's input in green:
This program will verify Goldbach's Conjecture from 4 to a number of your choosing. What should the top end of the range be? 22 4 = 2 + 2 6 = 3 + 3 8 = 3 + 5 10 = 3 + 7 = 5 + 5 12 = 5 + 7 14 = 3 + 11 = 7 + 7 16 = 3 + 13 = 5 + 11 18 = 5 + 13 = 7 + 11 20 = 3 + 17 = 7 + 13 22 = 3 + 19 = 5 + 17 = 11 + 11
Test your program by running it several times. Put your test runs and their output in comments at the end of your .py file
Name your file Goldbach.py. Be certain to begin your file with the following header:
# File: --name of file-- # Description: --a description of your program-- # Assignment Number: # # Name: --your name-- # EID: --your eid-- # Course Name: CS303E # # Unique Number: --your section number-- # # Date created: # Date last modified: # # Slip days used this assignment: # Total slip days used:If you used pair programming, include name, EID, course name, and unique number for both partners along with the other required information.
Use the turnin program to submit your file. The file must be turned in by October 26th at 11pm. If you use slip days, please notify your TA when you turn in your file.
If you used pair programming, both partners must have enough slip days left to cover any slip days you use. (e.g., If you use two, both partners must have at least two left to use.) Turn the assignment in using just one partner's turnin account. The grader will grade it and enter the grade for both partners.
Did you remember to: