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.
#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++;
}
}
Comments
Leave a comment