Title 1440:goldbach ' s conjecture
time limit:1 seconds
Memory limit:128 MB
Special question: No
-
Title Description:
-
Goldbach ' s conjecture:for any even number n greater than or equal t o 4, there exists at least one pair of prime numbers P1 and p2 such that n = p1 + p2.
This conjecture have not bee N proved nor refused yet. No One is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here's to write a program this reports the number of all the pairs of prime numbers satisfying the condition In the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to all number, the program should output the number of pairs mentioned above. Notice that we is interested in the number of essentially different pairs and therefore do should not count (P1, p2) and (P2, p1) separately as and different pairs.
-
Input:
-
An integer is given on each input line. You may assume this each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
-
Output:
-
Each of the output line should contain an integer number. No other characters should appear in the output.
-
Sample input:
-
610120
-
-
Sample output:
-
121
Problem-solving ideas: First find a certain range of primes, save up, and then traverse, find matching words, count++.
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<stack>#include<math.h>using namespacestd;# define Len200000intPrime[len];//used to store prime numbersintPrimesize;//number of vegetarian charactersBOOLMark[len];//Mark whether a number is prime voidinit () {primesize=0;//Number of characters initialized to 0 Long Long inti,j; for(i=0; i<len; i++) {Mark[i]=false;//true to indicate that it is not a prime number } for(i=2; i<len; i++) { if(mark[i]==true)Continue;//not primes continue Else{prime[primesize++]=i; for(J=i*i; j<len; j+=i) {mark[j]=true; } } }} intMain () {init (); intN; while(SCANF ("%d", &n)!=eof&&N) {intCount=0; for(intI=0; i<primesize&&prime[i]<n; i++) for(intJ=i; j<primesize&&prime[j]<n; J + +) { if(prime[i]+prime[j]==N) Count++; } printf ("%d\n", Count); } return 0;} /************************************************************** problem:1440 User:zhuoyuezai language:c++ result:accepted time:70 Ms memory:2496 kb****************************************************************/
Method 2, not Mister into a prime list, but a windfall judgment, seemingly short of time some
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<stack>#include<math.h>using namespacestd;BOOLJudgeintN) { if(n<=1) return false; intBound = (int) (sqrt (n) +1); for(intI=2; i<bound; i++) { if(n%i==0) return false; } return true; } intMain () {intN; while(SCANF ("%d", &n)!=eof&&N) {intCount=0; intm=n/2; for(intI=2; i<=m; i++) { if(Judge (i) &&judge (ni)) Count++; } printf ("%d\n", Count); } return 0;} /************************************************************** problem:1440 User:zhuoyuezai language:c++ result:accepted time:20 Ms memory:1532 kb****************************************************************/
Title 1440:goldbach ' s conjecture