Home > Back-end >  O goldbach conjecture
O goldbach conjecture

Time:09-16

CodePudding user response:

The simplest double circulation loop not came out

CodePudding user response:

For example
 int isPNum (int num) {//judge whether prime 
If (num==2) return 1;
Else if (num<2 | | num % 2==0) return 0;
Int n=num> 1;//to num half as a condition of end of cycle
For (int I=3; i<=n; I +=2) {
If (num % I==0) return 0;
}
return 1;
}

Int main ()
{
Int I, j, k;
For (I=4; i<=2000; I +=2) {//I from 4 to 2000 even
K=i> 1;//k=I/2 is a meaning (take I half as a condition of end of cycle)
For (j=2; j<=k; J++) {//j and I - j is decomposed two
If (j! I - j)={
If (isPNum (j) & amp; & IsPNum (I - j)) {
Can be decomposed into printf (" % d % d + % d \ n ", I, j, I, j);
break;//if you need to find out all the decomposition results the line commented out
}
} else {
If (isPNum (j)) {
Can be decomposed into printf (" % d % d + % d \ n ", I, j, j);
break;//if you need to find out all the decomposition results the line commented out
}
}
}
}
return 0;
}

CodePudding user response:

reference 1/f, 5250 response:
the simplest of double circulation loop once not came out
so I finally say to optimization

CodePudding user response:

refer to the second floor qybao response:
for example
 int isPNum (int num) {//judge whether prime 
If (num==2) return 1;
Else if (num<2 | | num % 2==0) return 0;
Int n=num> 1;//to num half as a condition of end of cycle
For (int I=3; i<=n; I +=2) {
If (num % I==0) return 0;
}
return 1;
}

Int main ()
{
Int I, j, k;
For (I=4; i<=2000; I +=2) {//I from 4 to 2000 even
K=i> 1;//k=I/2 is a meaning (take I half as a condition of end of cycle)
For (j=2; j<=k; J++) {//j and I - j is decomposed two
If (j! I - j)={
If (isPNum (j) & amp; & IsPNum (I - j)) {
Can be decomposed into printf (" % d % d + % d \ n ", I, j, I, j);
break;//if you need to find out all the decomposition results the line commented out
}
} else {
If (isPNum (j)) {
Can be decomposed into printf (" % d % d + % d \ n ", I, j, j);
break;//if you need to find out all the decomposition results the line commented out
}
}
}
}
return 0;
}

Here is a prime number is to be repeated use
Then play table is clearly a more cost-effective approach

CodePudding user response:

Should first prime playing table
And then use the table surface prime number two now add
Together and then judge the whether result all the even Numbers less than 2000 are covered in the

CodePudding user response:

reference 5 floor lin5161678 reply:
should first prime playing table
And then use the table surface prime number two now add
Together and then judge the whether result all the even Numbers less than 2000 are covered in the

That's a good idea, need not frequent to judge whether a prime
 int isPNum (int num) {//judge whether prime 
If (num==2) return 1;
Else if (num<2 | | num % 2==0) return 0;
Int n=num> 1;//to num half as a condition of end of cycle
For (int I=3; i<=n; I +=2) {
If (num % I==0) return 0;
}
return 1;
}

Int main () {
Int pn [2000]={0};//used to store the prime
Int pp [2000]={0};//used to quickly locate primes
Int I, j, FLG=0, CNT=1;//prime number
Pn [0]=2;
Pp [2] + +;

For (I=3; i<2000; I +=2) {//to find prime Numbers within 2000
{if (isPNum (I))
Pn [cnt++]=I;
Pp [I] + +;
}
}

For (I=4; I<2000; I +=2) {
For (j=0; jIf (pp/I - pn [j]] & gt; 0) {//pn [j] is prime, I - pn if [j] is also a prime number, then can break down
Can be decomposed into printf (" % d % d + % d \ n ", I, pn [j], I - pn [j]);
break;
}
}
If (j==CNT) {//j go to CNT note I can't break down
Flg++;//accumulation can not break down the number of
}
}
Printf (" 2000 even number greater than 2 within can be decomposed into the sum of two prime Numbers? \ n \ n the answer is: % s ", FLG?" No ":" Yes ");//can't break down the number is greater than 0 that the answer is No, otherwise it is Yes
}

CodePudding user response:

 # include & lt; Stdio. H> 

Int isPrimeNumber (int n) {
If (n<2) return 0;
If (n==2) return 1;
For (int I=2; iIf I (n %==0) return 0;
}
return 1;

}
Int main (void)
{
Int num.
Printf (" input a Even number (2 & lt; Number & lt; 2000) : ");
The scanf (" % d ", & amp; Num);
For (int I=2; iIf (isPrimeNumber (I) & amp; & IsPrimeNumber (num - I)) {
Printf (" Even number + % d % d=% d \ n ", num, I, num - I);
break;
}
}
return 0;
}
  • Related