Answer to Question #346477 in C++ for Justice

Question #346477

Jazmin encountered a problem in his mathematics class. His teacher announced that if any positive integer is equal to the sum of its proper positive divisors, i.e., the sum of its positive divisors excluding the number itself, such a number is called a perfect number. He told them to write fifty examples for such numbers. Ali understands that to find fifty examples will waste a lot of time, instead he will have to come up with a more efficient way to implement this. Help Ali implement such a system through your program.



1
Expert's answer
2022-06-01T12:54:58-0400
#include <iostream>
#include <cmath>


using namespace std;


int main() {
    int s = 0, number = 1, temp_sum;
    while (s != 50) {
        temp_sum = 0;
        for (int i = 2; i < sqrt(number); i++) {
            if (number % i == 0) {
                temp_sum += i + (number / i);
            }
        }
        if (temp_sum == number) {
            cout << number;
            s++;
        }
        number++;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS