// 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 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; }