Description
As we know, in 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:
Every even number greater than 4 can be written as the sum of two odd prime numbers.
There are various decompose methods for a certain number. The maximal difference of these two prime numbers is called MDP. For example, 20 = 3+17 = 7+ 13, both of the equation satisfies the Goldbach rule. Here the maximal difference is 17 – 3 =14, so the MDP of 20 is 14.
Your task is to calculate all the MDP of even number between 6 and 2012 inclusive. Output these numbers and their MDP order by MDP in ascending sort order. If the MDP is same, order by their value.
6 0 …… 100 94 …… 2012 1986 ……
Hint:Not all the numbers are listed in the sample. There are just some examples. The ellipsis expresses what you should calculate.
#include<stdio.h> int isPrime(int n)//判断是否为素数 { int i,flag=1; for(i=2;i<n;i++) if(n%i==0) { flag=0; break; } return flag; } int main() { int a[2012],b[2012];//a用来储存这个数字,b用来储存这个数字的MDP int i,j,temp,n=0,m=0; for(i=6;i<=2012;i+=2)//i要记得每次都要+2,而且这样写的好处就是,这样顺便就把这些结果从小到大排了一遍 for(j=2;j<=i/2;j++) if(isPrime(j)&&isPrime(i-j)) { a[n++]=i; b[m++]=i-j-j; break; } for(i=0;i<n-1;i++) for(j=i;j<n-1;j++) if(b[j]>b[j+1])//按照题目要求,根据MDP排序,本人较水,所以用了一个冒泡排序 { temp=b[j+1]; b[j+1]=b[j]; b[j]=temp; temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } for(i=0;i<n;i++) printf("%d %d\n",a[i],b[i]); return 0; }