IN JAVA - Implement the Sieve of Eratosthenes and use it to find all prime numbe
ID: 3805690 • Letter: I
Question
IN JAVA - Implement the Sieve of Eratosthenes and use it to find all prime
numbers less than or equal to one million. Use the result to
prove Goldbach's Conjecture for all even integers between four and
one million, inclusive.
Implement a method with the following declaration:
This function takes an integer array as its argument. The array
should be initialized to the values 1 through 1000000. The
function modifies the array so that only the prime numbers remain;
all other values are zeroed out.
This function must be written to accept an integer array of any
size. You must should output for all primes numbers between 1 and
1000000, but when I test your function it may be on an array of a
different size.
Implement a method with the following declaration:
This function takes the same argument as the previous method
and displays each even integer between 4 and 1000000 with two
prime numbers that add to it.
The goal here is to provide an efficient implementation. This
means no multiplication, divisioni, or modulus when determining if
a number is prime. It also means that the second method must find
two primes efficiently.
Output for your program: All prime numbers between 1 and 1000000
and all even numbers between 4 and 1000000 and the two prime
numbers that sum up to it.
Explanation / Answer
Implement a function with the following declaration:
void sieve(int array[], int num);
This function takes an integer array as its argument. The array
should be initialized to the values 1 through 1000000. The
function modifies the array so that only the prime numbers remain;
all other values are zeroed out.
This function must be written to accept an integer array of any
size. You must should output for all primes numbers between 1 and
1000000, but when I test your function it may be on an array of a
different size.
Implement a function with the following declaration:
void goldbach(int array[], int num);
This function takes the same argument as the previous function
and displays each even integer between 4 and 1000000 with two
prime numbers that add to it.
The goal here is to provide an efficient implementation. This
means no multiplication, division, or modulus when determining if
a number is prime. It also means that the second function must find
two primes efficiently.
Output for your program: All prime numbers between 1 and 1000000
and all even numbers between 4 and 1000000 and the two prime
numbers that sum up to it
Update 1: Goldbach's conjecture states that every even integer greater than 2 can be expressed as the sum of two primes.
dadapro (8)
i have this code which outputs all prime numbers up to one million, but how do i change it to output all the even number from 4 to one million
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream> #include <iomanip> #include <deque> using namespace std; void drainSieve(deque<bool>&); void displayPrimes(const deque<bool>&); const size_t N = 1000000; int main(int argc, char *argv[]) { deque<bool> sieve(N, true); // Find primes drainSieve(sieve); // Display primes from 0 .. N displayPrimes(sieve); return 0; } void drainSieve(deque<bool> &ps) { size_t N = ps.size(); ps[0] = ps[1] = false; for (size_t i = 2; i*i < N; i++) { if (ps[i] == true) { for (size_t j = i+i; j < N; j += i) { ps[j] = false; } } } } void displayPrimes(const deque<bool> &sieve) { size_t N = sieve.size(); for (size_t i = 0, n = 0; i < N; i++) { if (sieve[i] == true) { cout << setw(8) << i << " "; if ((++n % 8) == 0) cout << endl; } } }