Problem Description
Verify Goldbach conjecture that any even number of sufficiently large (>=4) can be represented by a sum of two primes.
Input
Enter an even n. (2<n<=10000)
Output
Find A, B make n=a+b
Where A and B are two primes, and a<=b.
Sample Input
4
100
Sample Output
2 2
3 97
1#include <iostream>2 3#include <math.h>4 5 using namespacestd;6 7 intPrimeintm)8 9 {Ten One intI,n; A - if(m==1)return 0; - theN= (int) sqrt (Double) m); - - for(i=2; i<=n;i++) - + if(m%i==0)return 0; - + return 1; A at } - - intMain () - - { - in intCount,i,number; - toCount=0; + - while(cin>>Number ) the * { $ Panax Notoginseng for(i=2; i<=number/2; i=i+1) - the if(Prime (i) &&prime (number-i)) + A { the +cout<<i<<" "<<number-i<<Endl; - $ Break; $ - } - the } - Wuyi return 1; the -}
Other code:
1#include <stdio.h>2#include <math.h>3 intPrime (intm)4 {5 intr = m/2;6 for(inti =2; I <= R; i++)7 if(m% i = =0)8 return 0;9 return 1;Ten } One intMain () A { - intn,i; - while(~SCANF ("%d",&N)) the { - for(i=2; i<=n/2; i++) - if(Prime (i) &&prime (ni)) { -printf"%d%d\n", i,n-i); + Break; - } + } A return 0; at}
Wuhan University of Science and Technology acm:1009: Chinese Edition C language Programming tutorial (second Edition) example 4.18