Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Goldbach's Conjecture help!

807589Jan 7 2009 — edited Jan 7 2009
Ok so I have the sieve part of the code down and I am stuck with the Goldbach method. I can use any help you can give me.

Here is the project:

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, 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.

This is the code with the unfinished GoldBach method that I have implemented, and really dont know how to finish it. For the sake of testing purposes I switched the number to 100 instead of 1000000.
public class Sieve{
	
	
	public static void main(String[] args){
		
		int[] nums = new int[100];
		
		for(int i = 0; i < nums.length; i++){
			nums[i] = i + 1;
		}
		
 		sieve(nums);
		
		for (int i : nums) {
			if (i != 0) {
				System.out.println(i);
			}
		}
		
		goldbach(nums);
	}
 
		public static void sieve(int[] nums){
			
			nums[0] = 0;
			
			for(int i = 1; i * i <= nums.length; i++){
				if (nums[i] != 0) {
					for(int k = i + nums; k < nums.length; k += nums[i]){				
nums[k] = 0;
}
}
}
} // sieve method closer

public static void goldbach(int[] nums){


int[] evens = new int[nums.length];
for(int i = 4; i < evens.length; i+=2){
evens[i] = i;
//this is where I left off, I know you have to make a second for loop, but not sure how to implement it...


}
}
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 4 2009
Added on Jan 7 2009
12 comments
255 views