ISC Practicals and Theory - Goldbach's Conjecture
Updated 21 Oct 2026
Goldbach's Conjecture says that every even number greater than $2$ can be written as the sum of two prime numbers. This program, written in the main method alone, takes one even number, displays every way of writing it as a sum of two primes, and gives an error message for any other input.
Definition
Goldbach's Conjecture: every even number $N$ greater than $2$ is the sum of two prime numbers.
A prime number has exactly two factors: $1$ and the number itself.
- $10 = 3 + 7 = 5 + 5$
- $28 = 5 + 23 = 11 + 17$
- $4 = 2 + 2$
A pair such as $3 + 7$ and $7 + 3$ is the same way of writing the number, so the program shows each pair once, with the smaller prime first. The number is valid only if it is even and greater than $2$; an odd number, $2$, or a number below $2$ is invalid input.
Algorithm
- Accept the number
n. Ifn <= 2ornis odd (n % 2 != 0), displayInvalid inputand stop. - Display the heading line and set
ways = 0. - For
afrom 2 ton / 2, find the other number of the pair,b = n - a. - Count the factors of
ainca: forifrom 1 toa, add 1 tocawhenevera % i == 0. Ifcais 2,ais prime, so count the factors ofbincbin the same way. - If
ca == 2andcb == 2, both numbers are prime: displayn = a + band add 1 toways. - After the loop, display the number of ways.
Program
The outer for loop in main tries each pair a and b, and the inner for loops count the factors to test for a prime. Only the pairs with a up to n / 2 are tried, so no pair is shown twice.
import java.util.Scanner;
public class Goldbach
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter an even number greater than 2: ");
int n = sc.nextInt(); // the even number
if (n <= 2 || n % 2 != 0)
{
System.out.println("Invalid input");
}
else
{
System.out.println("The number " + n + " can be written as the sum of two primes in these ways:");
int ways = 0; // number of pairs found
for (int a = 2; a <= n / 2; a++)
{
int b = n - a; // the other number of the pair
int ca = 0, cb = 0; // number of factors of a and of b
for (int i = 1; i <= a; i++)
{
if (a % i == 0)
ca++;
}
if (ca == 2) // test b only when a is prime
{
for (int i = 1; i <= b; i++)
{
if (b % i == 0)
cb++;
}
}
if (ca == 2 && cb == 2) // both are prime
{
System.out.println(n + " = " + a + " + " + b);
ways++;
}
}
System.out.println("Number of ways = " + ways);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
n | int | The even number entered by the user |
ways | int | Number of pairs of primes found |
a | int | The smaller number of the pair; loop counter from 2 to n / 2 |
b | int | The other number of the pair, n - a |
ca | int | Number of factors of a |
cb | int | Number of factors of b (counted only when a is prime) |
i | int | Loop counter that tries each possible factor |
Sample input and output
Sample 1
Enter an even number greater than 2: 28
The number 28 can be written as the sum of two primes in these ways:
28 = 5 + 23
28 = 11 + 17
Number of ways = 2Sample 2
Enter an even number greater than 2: 10
The number 10 can be written as the sum of two primes in these ways:
10 = 3 + 7
10 = 5 + 5
Number of ways = 2Sample 3
Enter an even number greater than 2: 9
Invalid inputSample 4
Enter an even number greater than 2: 2
Invalid inputDry run
Input: n = 10. The outer loop runs for a from $2$ to $5$.
| a | b = n - a | a prime? | b prime? | Pair shown | ways |
|---|---|---|---|---|---|
| 2 | 8 | true | false | No | 0 |
| 3 | 7 | true | true | 10 = 3 + 7 | 1 |
| 4 | 6 | false | (not tested) | No | 1 |
| 5 | 5 | true | true | 10 = 5 + 5 | 2 |
The loop ends after a = 5 (which is n / 2), and the program displays "Number of ways = 2", as in the second sample run.
Variations
- Range: "Accept two positive integers m and n. Display every even number between m and n and the ways it can be written as the sum of two primes."
- Recursive technique: "Write the prime test using a recursive method and use it to display the ways an even number is the sum of two primes."
- Twin primes: "Display all twin prime pairs in a given range."
- Prime numbers: "Display all prime numbers between m and n."