Starting from:

$25

CSE215 -  Programming Assignment 2 - Solved

For your second programming assignment, you're going to write a program to test Goldbach's Conjecture: the conjecture that any even integer>4 can be written as the sum of two odd primes.

Details
Your program should ask the user to enter a starting number (an even integer), and to enter how many numbers to test (a positive integer), and then try to find a way to write the first number as the sum of two primes. Assuming it succeeds, proceed to the next even integer and write that as a sum of two primes, then proceed to the next, and so on, until you've done so for the requested number of numbers. If you find an even integer that can't be written in this way, print it out and call the newspapers :)

You can write your program in C or C++.

1.      a copy of your program's source code; and

2.      its output for 500 even numbers beginning with two million (so from 2,000,000 to 2,000,998).

Notes
•        Write yourself a function that tests a single number to see if it's prime (the easiest way to see if N is prime it to try to divide N by every number from 2 to the sqrt(N). Or you can just test odd divisors, if N isn't even

•        Rather than dividing, you can use the modulus operator (%). Remember, if a is divisible by b, then a%b==0

•        The way you design your algorithm can have a dramatic affect on your program. Suppose you want to know if X is expressible as the sum of two odd primes:

◦ The slowest approach is to generate pairs of primes, add them, and see if they add to X. You'll do a lot of extra work, and for X around a million, your program will likely never finish

◦ A better approach is calculate the first prime(3), and then test (X-3) to see if it is prime. If it is, you're done with number X. Otherwise, generate the second prime (5) and test

(X-5); if (X-5) isn't prime, then try (X-7), (X-11), and so on until one of them is prime.

More products