I received some help for this assignment already and the code seemed to work but
ID: 3530835 • Letter: I
Question
I received some help for this assignment already and the code seemed to work but now I'm starting to realize that I'm not outputting everything that I should be (and not in the correct format). I think this is just the matter of fixing the following code. Implementing and proving the Sieve of Eratosthenes and Goldbach's Conjecture is IMPERATIVE. I'm also going to include the assignment description so please pay close attention to it as that's the only way I can rate 5 stars. The code I have so far is:
----------------------------------------------------------------------------------------
public class Prime {
public static void main(String[] args)
{
int[] array=new int[1000000];
for(int i=0;i<1000000;i++)
{
array[i]=i;
}
sieve(array);
goldbach(array);
}
public static void sieve(int[] array)
{
for(int i = 1; i < 1000000; i++)
{
boolean isPrime = true;
for(int j=2; j < array[i] ; j++)
{
if(array[i] % j == 0)
{
isPrime = false;
array[i]=0;
break;
}
}
if(isPrime)
System.out.print(array[i] + " ");
}
System.out.println("Prime numbers are: ");
for(int i=0; i< 1000000; i++)
{
if(array[i] != 0)
System.out.println(array[i]);
}
}
public static void goldbach(int[] array)
{
System.out.println("Even numbers are: ");
for(int i=4; i < 1000000; i++)
{
if((array[i]%2) == 0)
System.out.println(array[i]);
}
}
}
----------------------------------------------------------------------------------------
And the description is:
Implement the Sieve of Eratosthenes and use it to find all primenumbers less than or equal to one million. Use the result toprove Goldbach's Conjecture for all even integers between four andone million, inclusive.
Implement a method with the following declaration:
This function takes an integer array as its argument. The arrayshould be initialized to the values 1 through 1000000. Thefunction 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 anysize. You must should output for all primes numbers between 1 and1000000, but when I test your function it may be on an array of adifferent size.
Implement a method with the following declaration:
This function takes the same argument as the previous methodand displays each even integer between 4 and 1000000 with twoprime numbers that add to it.
The goal here is to provide an efficient implementation. Thismeans no multiplication, division, or modulus when determining ifa number is prime. It also means that the second method must findtwo primes efficiently.
Output for your program: All prime numbers between 1 and 1000000and all even numbers between 4 and 1000000 and the two primenumbers that sum up to it.
Here's how a Sieve of Eratosthenes works:
http://s24.postimage.org/ah4qigcrp/sieve.gif
----------------------------------------------------------------------------------------
Explanation / Answer
Hey, I once implemented the Sieve of Eratosthenes for my class lab in java. Here is how I did it
Please rate with 5 stars if you like it :)