+ 2

What can be a program for goldbatch number in java

gold batch number consist of Every even integer greater than 2 can be expressed as the sum of two primes . Ex set of 14 are 7+7 and 11+3

16th Feb 2018, 1:49 PM
Vaibhav Arya
Vaibhav Arya - avatar
4 Answers
+ 3
import java.util.Scanner; public class Goldbachseries { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println (" We want a Java program to find the two prime numbers which sum up to a given number.ie goldbatch series \n "); System.out.println("Enter a +ve even no. \n"); System.out.print(n); int c1=0, p1=0, c2 =0,p2=0,num=0; if(n <= 2 && n%2!=0){ System.out.println("Wrong input"); System.exit(0); }else{ for(int i = 1;i<=n/2;i++){ for(int j = 1;j<= i;j++){ if(i%j==0) c1++; } if(c1==2){ p1=i; for(int k= 1;k<=n;k++){ for(int l = 1; l<= k;l++){ if(k%l==0) c2++; } if(c2==2){ p2=k; if(p1+p2==n){ System.out.println(" = "+p1+" + "+p2); num++; } } c2=0; } } c1=0; } } System.out.println("\n "+num+" combinations are there below "+n+"\n \n And don't forget to leave a like"); } }
19th Feb 2018, 9:04 AM
Vaibhav Arya
Vaibhav Arya - avatar
+ 2
yes
17th Feb 2018, 5:21 PM
Vaibhav Arya
Vaibhav Arya - avatar
+ 1
There can be more efficient methods. As a starting point, you can find all pairs of numbers sum up to given number and check if they both are prime.
17th Feb 2018, 5:26 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar
0
So you want a Java program to find the two prime numbers which sum up to a given number?
17th Feb 2018, 5:17 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar