Goldbach Number in Java
In this section, we will learn what is a Goldbach number and also create Java programs to check if the given number is Goldbach or not. The Goldbach number Java program frequently asked in Java coding tests to check the logic of the programmer.
In 1742, German mathematician Christian Goldbach proposed speculation that all prime numbers that are greater than 2 can be expressed as the sum of 2 odd prime numbers. It is speculation because there is no proof for the same.
A positive and even number is called a Goldbach number if the number can be expressed as the sum of two odd prime numbers. Note that all even integers greater than 4 are Goldbach numbers.
Goldbach Number Example
Consider the following numbers:
6 = 3 + 3
14 = 7 + 7 and 11 + 3
30 = 23 + 7, 11 + 19, and 13 + 17
28 = 11 + 17
12 = 7 + 5
10 = 5 + 5 and 7 + 3
We see that the numbers are positive and even, expressed as the sum of odd numbers.
Steps to Find Goldbach Number
Let's implement the above steps in a Java program.
Goldbach Number Java Program
We will solve the problem using recursion. In the following program, we have defined a method isPrime() to check if the number is prime or not.
Let's see another logic to find the Goldbach number.