A Goldbach number is one that can be expressed as the sum of two odd primes
All even number greater than 4 are Goldbach numbers
Example
6=3+3
3 and 3 are Goldbach pairs of 6
Here is the java program to find the Goldbach plairs between 9 and 50
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
if((n>9)&&(n<50)&&(n%2==0))
{
for(int x=1;x<=n;x+=2)
{
for(int y=x;y<=n;y+=2)
{ if((x%2!=0)&&(y%2!=0))
if((f1(x))&&(f1(y)))
if((x+y)==n)
{
System.out.println("the numbers are"+x+"+"+y+"="+n);
}
}
}
}
else
{
System.out.println("invalid");
}
}
public static boolean f1(int t)
{
int s=0;
for(int x=1;x<=t;x++)
{
if(t%x==0)
s=s+1;
}
if(s==2)
return true;
else
return false;
}
}
No comments:
Post a Comment