feedback

(Solution document) I have an error message when I run the code.


Please help me with my code. I have an error message when I run the code. If you can please edit or change the whole code and if you can please explain the process of each step in comments and the code, that would be better. Please see the question below and my code after the 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: public static void sieve(int [] array);


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 output for all prime 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: public static void goldbach(int [] array);


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, division, or modulus when determining if a number is prime. It also means that the second method must find two primes efficiently.


Outputs 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. Use command line arguments.


import java.math.BigInteger;


public class GoldbachsConjecture {

public static void main(String[] args) {

int n=Integer.parseInt(args[0]);

int array[]=new int[n];

//The array should be initialized to the values 1 through 1000000.

for(int i=0;i

{

array[i]=i;

}

sieve(array);

System.out.println("After performing Sieve: ");

display(array);

int array1[]=new int[n];

int start=4;

for(int i=0;i

{

array1[i]=start++;

}

System.out.println("After goldbach: ");

System.out.println("Prime numbers from 4 t two prime numbers that sum it up: ");

goldbach(array1);

}

/*

The function modifies the array so that only the prime numbers remain;

all other values are zeroed out.

*/

private static void sieve(int[] array) {

for(int i=0;i

{

if(!IsPrime(array[i]))

array[i]=0;

}

}

//to check a number is prime or not

private static boolean IsPrime(int num) {

BigInteger val = BigInteger.valueOf(num);

boolean status = val.isProbablePrime(1);

return status;

}

//displays array elements

private static void display(int[] array) {

for(int i=0;i

{

System.out.print(array[i]+" ");

}

System.out.println(" ");

}

//displays even number from 4 and the two primes make up it

private static void goldbach(int[] array) {

for(int i=0;i

{

System.out.println("");

if(isEven(array[i]))

{

System.out.print(array[i]+"tt");

sumOfPrime(array,array[i],i);

}

}

}

// to check a number is even

//Here repeated subtraction is used

private static boolean isEven(int number) {

boolean result=false;

while(number>=2)

{

number-=2;

}

if(number==0)

result=true;

return result;

}

//to find the two prime numbers that make up an even number

private static void sumOfPrime(int[] array, int number, int i) {

for(int j=0;j

{

for(int k=0;k

{

if(IsPrime(array[j])&& IsPrime(array[k]) && array[j]+array[k]==number)

{

System.out.print(array[j]+" "+array[k]);

i=0;

break;

}

}

}

}

}

 







About this question:

Pay using PayPal (No PayPal account Required) or your credit card. All your purchases are securely protected by .
SiteLock