/* Lab : Lab4 Day : Tuesday [ 19 Aug 08 ] Problem Number: 01 Problem Title : Goldbach's Conjecture Theory : Let we have to test Goldbach's Conjecture for a variable of name num. Then we have to do following Generate every possible pair of positive numbers whose sum is equal to the number num. and for every such pair of numbers (let us call numbers in pair as firstNum and secondNum) test Whether both number are prime or not Testing prime number As stated in problem if a number n is not devided by any number from 3 to n/2 the it is considered as prime Therefore initially we assume that the number is prime as nIsPrime = 1; then we generate all numbers from 3 to n/2 and test whether n is divisible by any one of them if n is divisible by any one of them then we set nIsPrime = 0; */ class sol_lab4_prob01{ public static void main(String args[]) { int num; // The number to be tested for Goldbach's Conjecture int i; // loop counter num = 50; // assign some value to num System.out.println("Number : "+num + " can be expressed as"); // Loop to generate all possible psitive pairs of numbers (firstNum + secondNum) whose sum is equal to num for( i = 2 ; i < num / 2 ; i = i + 1 ) { int firstNum; int secondNum; int j; int firstNumIsPrime; int secondNumIsPrime; firstNum = i; secondNum = num - i; firstNumIsPrime = 1; secondNumIsPrime = 1; // Testing whether firstNum is prime for( j = 3 ; j <= firstNum/2 ; j++ ) { if( ( firstNum % j ) == 0 ) { firstNumIsPrime = 0; break; } } // Testing whether secondNum is prime for( j = 3 ; j <= secondNum/2 ; j++ ) { if( ( secondNum % j ) == 0 ) { secondNumIsPrime = 0; break; } } // when both (firstNum & secondNum) are prime then print them if( ( firstNumIsPrime == 1 ) && ( secondNumIsPrime == 1) ) { System.out.println(num + " = " + firstNum + " + " + secondNum); } } } }