Tuesday, June 11, 2013

Program for Christian Goldback

PROGRAM
Christian Goldback has conjectured for N<1014 that every even N>2, can be represented as sum of two primes. For example, 18=5+13.

WAP that reads N which should be an even number>2 and represents it as sum of two primes.
-----------------------------------------------------------------------------------------------------

/**
 * class ChristianGoldback find two prime numbers whose sum is equal to a given number.
 * @author: Nitendra Verma 
 * @version: June 10,2013
 */
import java.util.Scanner;
public class ChristianGoldback
{
 int n,count=0;  //instance variables
    public void getnum(int nn) //method to assign parameter to number 
    {
        n=nn;
    }
    
    public void check()  //method to find prime numbers whose sum is equal to given number
    {
        int g1[]=new int[n];
        int flag=0;       
        for(int i=3;i<n;i++)  //outer loop
        {
            flag=1;
        for(int j=2;j<i;j++)  //inner loop
        {
           if(i%j==0)
           {
               flag=0;
               break;
            }           
        }
        if(flag==1)
        {
            g1[count]=i;
            count++;
        }
        
    }
        System.out.println("Number can be represented as sum of two primes:");
        for(int i=0;i<count/2;i++)
        {
            for(int j=0;j<count;j++)
            {
            if((g1[i]+g1[j])==n)
            {
                System.out.println(n +"=" +g1[i]+"+" +g1[j]);
            }
            }
        }
    }
    
    public static void main()
    {
        ChristianGoldback ob=new ChristianGoldback(); //creating object of class ChristianGoldback
        Scanner in=new Scanner(System.in);   ////creating object of class Scanner
        System.out.println("Enetr a num");
        int num=in.nextInt();   
        ob.getnum(num);     //calling method
        ob.check();         //calling method
    }
}

OUTPUT
Enetr a num
18
Number can be represented as sum of two primes:
18=5+13
18=7+11


/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in 




No comments:

Post a Comment

Ur comments r most welcome...