Test Goldbach conjecture for all even numbers in Java

Problem:

Christian Goldbach (1690–1764) conjectured in 1742 that every even number greater than 2
is the sum of two primes. Write a program that tests the Goldbach conjecture for all even
numbers less than 100. Use the Primes class from Problem 2.22. Your first 10 lines of output
should look like this:

Output:

4 = 2+2
6 = 3+3
8 = 3+5
10 = 3+7 = 5+5
12 = 5+7
14 = 3+11 = 7+7
16 = 3+13 = 5+11
18 = 5+13 = 7+11
20 = 3+17 = 7+13
22 = 3+19 = 5+17 = 11+11

Solution:

public class TestGoldbach {
public static void main(String[] args) {
Primes.setSize(1000);
System.out.println("4 = 2+2");
for (int n = 6; n < 100; n += 2) {
System.out.print(n);
for (int p = 3; p <= n/2; p += 2) {
if (Primes.isPrime(p) && Primes.isPrime(n-p)) {
System.out.print(" = "+p+"+"+(n-p));
}
}
System.out.println();
}
}
}


2 comments :

  1. ultrasonic liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete
  2. tummy tuck liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact