Home » Arrays(Dont Mess With Me) » Goldbach Number

Goldbach Number


import java.util.*;

public class Goldbach
{
 public static void main(String [] args)
 {
 Scanner scan=new Scanner(System.in);

 System.out.println("Enter Ending Range:");
 int endRange=scan.nextInt();

 System.out.println("Goldbach's Conjecture to Range=:");
 for(int num=4;num<=endRange;num=num+2)
 {
 int firstPrime=2;
 int secondPrime=0;
 while(true) // means that i m not able to find prime continues while loop.....
 {
 secondPrime=num-firstPrime;
 if(!isPrime(secondPrime))
 {
 firstPrime=getNextPrime(firstPrime);
 }


 if(num==(firstPrime+secondPrime)&&isPrime(secondPrime)&&isPrime(firstPrime))
 {
 System.out.println(num+"\t= "+firstPrime+" + "+secondPrime);
 break;
 }

 } // while....

 }
 }
 public static int getNextPrime(int num)
 {
 for(int i=num+1;;i++)
 {
 for(int j=2;j<=i;j++)
 {
 if(i%j!=0)
 {
 return i;
 }
 }
 }
 }
 public static boolean isPrime(int num)
 {
 for(int i=2;i<num;i++)
 {
 if(num%i==0)
 {
 return false;
 }
 }
 return true;
 }
}

&nbsp;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Website

arpit tak

arpit tak

I like JAVA . I code. I chill. I blog.I eat. I sleep. I dream.

Personal Links

View Full Profile →

Followers

%d bloggers like this: