ISC practical papers › ISC 2018 › Question 1
Goldbach Numbers
ISC 2018 Computer Science Practical, Question 1 · Number programs
The question
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, 5 and 5.
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.
Question: ISC 2018 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class ISC2018Q1{
private static int primes[]={3,5,7,11,13,17,19,23,29,31,37,41,43,47};
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.print("INPUT: N = ");
int N = in.nextInt();
System.out.println("OUTPUT: ");
if( N>9 && N<50){
if(N%2==0){
System.out.println("PRIME PAIRS ARE:");
int half=N/2;
for( int i=0; primes[i]<=half; i++){
if( isPrime(N-primes[i])){
System.out.println(primes[i]+", "+(N-primes[i]));
}
}
}else{
System.out.println("INVALID INPUT. NUMBER IS ODD");
}
}else{
System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
}
in.close();
}
public static boolean isPrime(int number){
// for( int i=0;i<primes.length;i++){
// if(number==primes[i]) return true;
// }
for(int prime: primes){
if(number==prime) return true;
}
return false;
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.