Home > Back-end >  For the bosses to help me take a look at this perfect number (an integer does not include all factor
For the bosses to help me take a look at this perfect number (an integer does not include all factor

Time:09-29

CodePudding user response:

The leakage part of factor
 bool isPerect (int n) {
Int m;
int sum=0;
For (m=1; M<=n/2; + + m) {//cycle to half n only because of you, more than half of n factor missed the accumulative
If (n % m==0) {
The sum +=m;
If (m! Sum=n/2) +=n/m;//missed the factor here
}
}
If (sum==n) return true;
return false;
}

CodePudding user response:

Rectify the
Int sum=1;
For (m=2; M<=n/2; 2 + + m)//start cycle, otherwise==0 n % 1, sum +=n/m have a problem, so take out 1 cycle

CodePudding user response:

refer to the second floor qybao response:
correct you
Int sum=1;
For (m=2; M<=n/2; 2 + + m)//start cycle, otherwise==0 n % 1, sum +=n/m have a problem, so take out 1 cycle

But I 6,28,8128 jumped out of the first three perfect number are, 33550336 jump out

CodePudding user response:

Jump out is running slow

CodePudding user response:

Perhaps more than int scope, try the unsigned

CodePudding user response:

2 and 4 floor positive solution
That number is a program is running slowly, and you won't come after all is traversing 30 million digital
You can see in the output 8128 after the program is still in operation, and does not show "please press any key to end"

CodePudding user response:

So is there any solution

CodePudding user response:

Code:
# include & lt; iostream>
# include & lt; cmath>
# include & lt; Algorithm>
using namespace std;

Bool judge (int n) {
Int flag=n % 10;
If (flag!=6 & amp; & Flag! Return 0=8);
Int sum=1;
For (int I=2; i<=SQRT (n); I++) {
If I==0) (n % {
The sum +=I;
The sum +=(n/I);
}
}
If (sum==n) return 1;
return 0;
}

Int main () {
int n;
Cin & gt;> n;
for(int i=1; i<=n; I++) {
If (judge (I)) cout & lt; }
return 0;
}

CodePudding user response:

In addition, 496 is also a perfect number,
  • Related