Loops && Booleans

Your Assignment

The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture.

You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number.

An easy way to map out this though is to write out a table. If I am testing Golbach’s conjecture for the input number of 36 (an even, non-prime number), we run through the possible combinations of numbers less than 36 and are prime, that when summed will equal 36.

Goldbach’s conjecture for 36
num i j
36 1 35 (num - i)
36 2 34 (num - i)
36 3 33 (num - i)
36 4 32 (num - i)
36 5 31 (num - i)
36 35 1 (num - i)

Assignment Requirements

Three functions are required:

  • get_input():

    This function takes no parameters, but will ask the user for an integer number. It will return a valid integer. You will use an indefinite loop and exception handling like we discussed in class. It is required to catch user input error and gracefully recover. Learn about the continue keyword and how it works in a loop; it can be handy.

  • is_prime(n):

    This function will take an integer parameter n, and return True if the number is prime, or return False if it is not prime. You can use this pseudocode for primality testing to write this function.

  • main():

    This does the bulk of your program’s work for solving Goldbach’s conjecture. You will call get_input() and is_prime(n) from main().

    Goldbach’s conjecture itself will be solved using an indefinite loop. Particularly a ‘post-test’ loop pattern. A for loop will not be accepted as part of the solution.


A successful run:

This program tests the Goldbach's conjecture
Please enter an even integer larger than 2: 36
36 = 5 + 31

A run recovering from errors:

This program tests the Goldbach's conjecture
Please enter an even integer larger than 2: 7
Wrong input!
Please enter an even integer larger than 2: foo
Bad input!
Please enter an even integer larger than 2: 8
8 = 3 + 5

If by chance the number chosen is valid, but doesn’t hold for Goldbach’s conjecture, then print:

Goldbach's conjecture doesn't hold for [whatever the number is]

(I’m not aware of a number for which this occurs, but handle this possible case anyways.)

Submission Requirements

Your file is required to be named lab_loops_booleans.py.

Please read and follow the Submission Requirements to avoid point deductions specified below.


The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Note: There may be additional criteria for grading, this is just a summary of the major rubric items.

Grading Rubric
Type Description Point Loss (Max)
SyntaxError Exceptions A program that doesn’t even start due to syntax errors. -30
Docstrings Missing proper lab header (e.g. no name, lab section, etc) -10
main() Your program should have main() as the first-called function -10
PEP 8 Style Egregious problems with your code styling -10
Incorrect Filename Follow the submission instructions -5
Commenting Insufficient or excessive comments -5
  [Lab Specific]  
Functions Three functions are required: get_input(), is_prime(n), and main -10 each/missing
Indefinite Loop The code for get_input() and main() is to be written using a while loop, only -15
Logic Boolean logic errors for if, and while conditions -5/each

Extra Credit

Extra credit work must be submitted in what would be your original submission file for the assignment. All extra credit code must be clearly commented in order to receive any points.

  • +5 points

    Modify your program to read in a series numbers from a text file, or ask the user for one number, depending on what the user wants.

  • +5 points

    Print all possible combinations of prime numbers that solve the conjecture (commutative results aren’t required, e.g. 5+3 and 3+5 are the same combination in my view).