-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompleteNumber.c
executable file
·84 lines (78 loc) · 1.2 KB
/
completeNumber.c
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
#include <stdio.h>
int lastPrime = 1;
int intSqrt(int n)
{
if (n == 0)
{
return 0;
}
else
{
int i = 1;
while (i * i <= n)
{
i++;
}
return i - 1;
}
}
int isPrime(int n)
{
int ul = intSqrt(n);
for (int i = 2; i <= ul; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
int getPrime(void)
{
lastPrime++;
while (!isPrime(lastPrime))
{
lastPrime++;
}
return lastPrime;
}
int isCompleteNum(int orig)
{
int p, sum = 0, n = orig;
// scanf("%d", &n);
// printf("%d=", n);
while (n != 1)
{
p = getPrime();
while (n % p == 0)
{
n /= p;
}
sum += p;
}
lastPrime = 1;
if (sum + 1 == orig)
{
return 1;
}
else
{
return 0;
}
}
int main(void)
{
for (int i = 1; i <= 1000; i++)
{
if (isCompleteNum(i))
{
printf("%d", i);
if (i != 1000)
{
printf(" ");
}
}
}
return 0;
}