How To Check Whether a Number is a Goldbach Number or Not in Java?



What is Goldbach Number?

A number is said to be a Goldbach number if the number can be expressed as the addition of two odd prime number pairs. For example, let's consider the number 98 and find the pairs of prime numbers (19, 79). If you calculate the sum of 19 + 79, you get 98, so 98 is a Goldbach number.

If we follow the above condition, then we can find that every even number larger than 4 is a Goldbach number because it must have any pair of odd prime number pairs. But the odd numbers are not satisfying because we know the addition of two numbers can never be an odd number.

Input & Output Scenarios

Below are a few input and output scenarios which help you to understand the problem in a better way:

Scenario 1

Suppose the input number is 50:

Input: 50
Output: Yes
(3 , 47)
(7 , 43)
(13 , 37)
(19 , 31)

As we notice here we got some pairs of odd prime numbers whose addition value is equal to the 50. Hence, 50 is a Goldbach number.

Scenario 2

Suppose the given number is 47:

Input: 47
Output: No

We noticed that we didn't find any pairs of odd prime numbers whose sum is equal 47, therefore, 47 is not a Goldbach number.

Example 1

The following example checks whether the given number 30 is a Goldbach number. We get the pairs of all odd numbers between the range 2 to 30 and calculate the sum, which will be compared with a number for checking the Goldbach number:

public class checkGoldbach {
   public static void main(String args[]) {
      int b = 0, sum = 0;
      
      int num = 30;
      System.out.println("The given number is: " + num);
      int temp = num;
      
      int arr1[] = new int[num];
      int arr2[] = new int[num];
      int count = 0;
      //check whether the number is even or
      if(num %2 != 0) {
         System.out.println(num + " is not a Goldbach number.");
      }
      else {
         for(int i = 1; i <= num; i++) {
            for(int j = 1; j <= i; j++) {
               if(i % j == 0) {
                  count++;
               }
            }
            
            //find the odd prime numbers
            if((count == 2) && (i % 2 != 0)) {
            
               //stores odd prime numbers into first array
               arr1[b] = i;
               
               //stores odd prime numbers into second array
               arr2[b] = i;
               b++;
            }
            count = 0;
         }
         //print the odd prime number pairs
         System.out.println("Odd Prime Pairs are: ");
         
         for(int i = 0; i<b; i++) {
            for(int j = i; j<b; j++) {
            
               //find the sum of two odd prime numbers
               sum = arr1[i] + arr2[j];
               
               //condition for comparing the sum value with input number
               if(sum == temp) {
               
                  //print pair of odd prime numbers
                  System.out.print("(" + arr1[i]+" , "+arr2[j] + ")");
                  System.out.println();
               }
            }
         }
         //print the final result if it is Goldbach number
         System.out.println( "Yes! " + temp + " is a Goldbach number.");
      }
   }
}

Output

Following is the output of the above program:

The given number is: 30
Odd Prime Pairs are: 
(7 , 23)
(11 , 19)
(13 , 17)
Yes! 30 is a Goldbach number.

Example 2

This is another example of checking the Goldbach number. We define a method named checkGoldbach(), which gets the pairs of all prime numbers between the range of 2 to 98 and calculates the sum that will be compared with the given number:

public class goldbachNumber {
   static boolean checkGoldbach(int num) {
      int b = 0, count = 0, sum = 0;
      int arr1[] = new int[num];
      int arr2[] = new int[num];
      int temp = num;
      //check whether the number is even or
      if(num %2 != 0) {
         return false;
      }
      else {
         for(int i = 1; i <= num; i++) {
            for(int j = 1; j <= i; j++) {
               if(i % j == 0) {
                  count++;
               }
            }
            
            //find the odd prime number
            if((count == 2) && (i % 2 != 0)) {
               arr1[b] = i;
               arr2[b] = i;
               b++;
            }
            count = 0;
         }
         System.out.println("Odd Prime Pairs are: ");
         for(int i = 0; i < b; i++) {
            for(int j = i; j < b; j++) {
            
               //find the sum of two odd prime numbers
               sum = arr1[i] + arr2[j];
               
               //condition for comparing the sum value with input number
               if(sum == temp) {
                  
                  //print pair of odd prime numbers
                  System.out.print("(" + arr1[i]+ " , " + arr2[j] + ")");
                  System.out.println();
               }
            }
         }
      }
      return true;
   }
   
   public static void main(String args[]) {
      int num = 97;
      System.out.println("The given number is: " + num);
      boolean result = checkGoldbach(num);
      if(result) {
         System.out.println("Yes! " + num +" is a Goldbach number.");
      } else {
         System.out.println("No! " + num  + " is not a Goldbach number.");
      }
   }
}

Output

The above program produces the following output:

The given number is: 97
No! 97 is not a Goldbach number.
Updated on: 2025-06-13T12:26:57+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements