Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

its running too slow or is erroring. either way its not finishing on my schedule

ID: 3622293 • Letter: I

Question

its running too slow or is erroring. either way its not finishing on my schedule. Also I want the amount up to to the 44th perfect number. can I do that in C++ . can you show me how?



[code]#include<iostream>
using namespace std;

int main() {
int num;
int sum = 0;

cout << "Find all the perfect numbers up to number: ";
cin >> num;

for (int i = 1; i <= num; i++) { //check every number from 1 to num
for (int j = 1; j < i; j++) { //check every number from 1 to i-1 for divisibility
if (i % j == 0) // if j divides evenly into i
sum += j; //add j (the divisor) to the sum
}
if (sum == i) //if the sum of the divisors equals the number
cout << i << endl; //print out the perfect number

sum = 0; //reset the sum
}

cout << "DONE!" << endl;

return EXIT_SUCCESS;
}
[/code]

Explanation / Answer

please rate - thanks

#include<iostream>
using namespace std;

int main() {
int num;
int sum = 0;

cout << "Find all the perfect numbers up to number: ";
cin >> num;

for (int i = 1; i <= num; i++) { //check every number from 1 to num
sum = 0;
for (int j = 1; j < i; j++) { //check every number from 1 to i-1 for divisibility
if (i % j == 0) // if j divides evenly into i
sum += j; //add j (the divisor) to the sum
}
if (sum == i) //if the sum of the divisors equals the number
      cout << i << endl; //print out the perfect number


}

cout << "DONE!" << endl;
system("pause");
return EXIT_SUCCESS;
}

-------------------------------------

I think I once did this years ago with someone else, and it took days to get 44 numbers

#include<iostream>
using namespace std;

int main() {
int i=1;
int sum = 0;
int count=0;

while(count<44)
{
sum = 0;
for (int j = 1; j < i; j++) { //check every number from 1 to i-1 for divisibility
    if (i % j == 0) // if j divides evenly into i
         sum += j; //add j (the divisor) to the sum
    }
if (sum == i) //if the sum of the divisors equals the number
      {cout << i << endl; //print out the perfect number
      count++;
      }
i++;
}
cout << "DONE!" << endl;
system("pause");
return EXIT_SUCCESS;
}