Programming and Computer Science in Java:
Quiz #4a:   Retake of Test #1, Question 8/9/10.
David Kosbie, 2002-2003
See Course Home Page

Quiz Date:  Wed, Mar 12, 2003

This document is also available as quiz4a.doc.

• This is a written quiz, you may not use JCreator.

•  No notes, no books, no calculators, no talking, etc

• Good luck!

Question 1:  Goldbach's Conjecture states that every even number larger than 2 is the sum of two prime numbers.  Interestingly, nobody has ever proven it to be true, but it sure seems to be true.  Consider the following examples:

4 = 2 + 2

6 = 3 + 3

8 = 3 + 5

10 = 3 + 7

12 = 5 + 7

14 = 3 + 11

16 = 3 + 13

...

Write a Java program which empirically confirms Goldbach's Conjecture by repeatedly reading in an even integer n (halting when n < 4) and printing out that number as the sum of two primes (of course, your program must find the primes which sum to n).  If n is odd, just print out that it is odd.

Here is exactly how your program should work:

Enter an even integer [<4 to exit]: 10
10 = 5 + 5

Enter an even integer [<4 to exit]: 17
17 is odd.  Please try again.
Enter an even integer [<4 to exit]: 16
16 = 3 + 13
Enter an even integer [<4 to exit]: 0
Goodbye!

Hint:  for each number p which is less than n, check if p is prime, then check if (n-p) is also prime.  If both are true, then p and (n-p) are the two primes which sum to n, because p + (n-p) == n.

Hint:  Even if you cannot solve this problem outright, write as much of the solution as you can!

See Course Home Page