-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPerfect_Number.cpp
More file actions
97 lines (94 loc) · 2.37 KB
/
Perfect_Number.cpp
File metadata and controls
97 lines (94 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//MingkuanPang
//This fucntion can tell What
//numbers below 5000 are perfect ? What odd numbers
//below 5000 are abundant ? What are the relative
//proportions of deficient, abundant, and perfect numbers ?
#include <stdio.h>
#include<iostream>
using namespace std;
int NUMBER = 5000;
bool if_perfect(int);
bool if_deficient(int);
bool if_abundant(int);
bool if_odd(int);
int my_sum(int);
int main()
{
double num_perfect = 0, num_deficient = 0, num_abundant = 0;
int num_in_NUMBER;
cout << "The perfect numbers in 5000 are: ";
for (num_in_NUMBER = 1; num_in_NUMBER <= NUMBER; num_in_NUMBER++)
{
if (if_perfect(num_in_NUMBER))
{
cout << num_in_NUMBER << " ";
num_perfect++;
}
else
if (if_deficient(num_in_NUMBER))
num_deficient++;
else
if (if_abundant(num_in_NUMBER))
num_abundant++;
}
cout << endl;
cout << "These odd numbers below 5000 are abundant: ";
for (num_in_NUMBER = 1; num_in_NUMBER <= NUMBER - 1; num_in_NUMBER++)
if (if_abundant(num_in_NUMBER) and if_odd(num_in_NUMBER))
cout << num_in_NUMBER << " ";
cout << endl;
cout << "The percentage of perfect number in 5000 is :" << num_perfect / NUMBER * 100 << "%" << endl;
cout << "The percentage of deficient number in 5000 is :" << num_deficient / NUMBER * 100 << "%" << endl;
cout << "The percentage of abandant number in 5000 is :" << num_abundant / NUMBER * 100 << "%" << endl;
return 0;
}
int my_sum(int factor)//This function can cacaulate the sum of all divisor in a number
{
int num_in_factor, sum = 0;
for (num_in_factor = 1; num_in_factor <= factor - 1; ++num_in_factor)
if (factor%num_in_factor == 0)
sum += num_in_factor;
return sum;
}
bool if_perfect(int factor)//This function can determind if a number is a perfect number
{
int sum;
bool real;
sum = my_sum(factor);
if (sum == factor)
real=1;
else
real=0;
return real;
}
bool if_deficient(int factor)//This function can determind if a number is a deficient number
{
int sum;
bool real;
sum = my_sum(factor);
if (sum < factor)
real=1;
else
real=0;
return real;
}
bool if_abundant(int factor)//This function can determind if a number is a abundant number
{
int sum;
bool real;
sum = my_sum(factor);
if (sum > factor)
real=1;
else
real=0;
return real;
}
bool if_odd(int factor)//This function can determind if a number is an odd number
{
bool real;
if (factor % 2 == 1)
real=1;
else
real=0;
return real;
}