blob: c0c9cfc6efaf889b5202ff5e5cddad9e6380d01e (
plain)
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
|
// Programmieren 1, Praktikum 3, Aufgabe 3
// Sven Eisenhauer
// 17.11.2004
//
// file: main.cpp
//
// purpose: find "perfect numbers": The sum of all integer factors of a number is the number.
//
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int isPerfect(int);
int main()
{
// how far to search
const int maxNumbers=1000;
for (int j=1;j<=maxNumbers;j++)
{
if (0 == isPerfect(j))
{
cout << ": " << j << endl;
}
}
cout << "\n";
return 0;
}
int isPerfect(int number)
{
int sum=0;
// check for all numbers between the number, exclusive the number itself and 1 inclusive
for (int i=number-1;i>=1;i--)
{
// is the actual number a factor???
if (0 == number%i)
// if it is, add it
sum+=i;
}
// the sum of all factors... and so on
if (sum == number)
{
// so we have a perfect number here... fine... find the factors again and cout them
for (int k=number-1;k>=1;k--)
if (0 == number%k)
cout << k << " ";
return 0;
}
else
return 1;
}
|