PROGRAM 4: GOLDBACH NUMBER PROGRAM IN JAVA

A GoldBach number is a positive even integer that can be expressed as the sum of two odd primes

Note: All even integer numbers greater than 4 are GoldBach numbers.

Example:

6             =             3 + 3

10           =             3+7

10           =             5+5

Hence 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7.

Write a program to accept an even integer ‘N’ where N>9 and N<50. Find all the odd prime pairs, whose sum is equal to the number ‘N’

 

Test your program with the following data and some random data.

 

Example 1

INPUT                  :              N=14

OUTPUT              :              PRIME PAIRS ARE: 3,11

                                                                                 7,7

Example 2

INPUT   :              N=30

OUTPUT:             PRIME PAIRS ARE             7 , 23

                                                                           11,19

                                                                           13,17

Example 3

INPUT   N=17

OUTPUT:             INVALID INPUT, NUMBER IS ODD

Example 4

INPUT   : N=126

OUTPUT              :              INVALID INPUT. NUMBER OUT OF RANGE.

 

ALGORITHM:

 

Step1: Start.

Step2: Accept a number ‘N’ from the user.

Step3: Check whether it lies within given range (between 9 to 50) and also check if it is an even integer (N%2==0).

Step4: Take a loop from 2 to N/2(to avoid repetition of pairs) and check for GoldBach Number.

Step5: In each iteration take i and N-i, sum of these numbers gives entered number N and check if both of them are prime using isPrime() method.

Step6: If isPrime() method returns true, condition for GoldBach is satisfied and both numbers are displayed as a prime pair in given format.

Step7: Repeat step 5 and 6 till all possible odd prime pairs are found.

Step8: Stop.

 

 

Program

import java.util.*;

class g_b

{

    Scanner sc=new Scanner(System.in);

    void main()

    {

       

        System.out.print("INPUT\t:\tN=");

        int N=sc.nextInt();

        if(N<10 || N>49)

        {

            System.out.println("OUTPUT:\tINVALID INPUT. NUMBER OUT OF RANGE.");

        }

        else if(N%2!=0)

        {

            System.out.println("OUTPUT:\tINVALID INPUT, NUMBER IS ODD");

        }

        else

        {

            System.out.print("OUTPUT\t:\tPRIME PAIRS ARE:"); 

            for(int i=3;i<=N/2;i=i+2)

            {

                int sum=N-i;

               

                if(isPrime(i)==true && isPrime(sum)==true)

                {

                    System.out.print(i + ", " + sum+"\n\t\t\t\t");

                    }

                }}

            }

        boolean isPrime(int n){

        int c = 0;

        for(int i = 1; i <= n; i++){

            if(n % i == 0)

                c++;

        }

        if(c == 2)

            return true;

            else

        return false;

    }}




Sample input/output:






Random input/output:

Comments

Popular posts from this blog