From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp (limited to 'Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp') diff --git a/Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp b/Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp new file mode 100644 index 0000000..c0c9cfc --- /dev/null +++ b/Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp @@ -0,0 +1,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 +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; +} \ No newline at end of file -- cgit v1.2.3