#include <iostream>
using namespace std;
const int MAX_NUM = 5000;
int sum_of_divisors(int n) {
int sum = 0;
for (int i = 1; i < n; ++i) {
if (n % i == 0) sum += i;
}
return sum;
}
int main() {
int perfect = 0, deficient = 0, abundant = 0, odd_abundant = 0;
for (int i = 1; i <= MAX_NUM; ++i) {
int sum = sum_of_divisors(i);
if (i == sum) {
cout << "Number " << i << " is perfect" << endl;
++perfect;
}
else if (i < sum) {
cout << "Number " << i << " is abundant" << endl;
++abundant;
}
else {
cout << "Number " << i << " is deficient" << endl;
++deficient;
}
if ((i < sum) && (i % 2 == 1)) {
cout << "Number " << i << " is odd abundant" << endl;
++odd_abundant;
}
}
cout << "A number of deficient, odd_abundant, abundant, perfect: "
<< deficient << ":" << odd_abundant << ":" << abundant << ":" << perfect << endl;
cout << "Relative proportion of deficient, odd_abundant, abundant, perfect: "
<< (float) deficient/(float) MAX_NUM << ":" << (float) odd_abundant/(float) MAX_NUM << ":" << (float) abundant/(float) MAX_NUM << ":" << (float) perfect/(float) MAX_NUM << endl;
}
Comments
Dear visitor, Please check the updated answer.
When you run this program it only gives you 1 perfect number and I know there is more than one perfect number . How do you fix it ?
Leave a comment