summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp')
-rw-r--r--Bachelor/Prog1/Prakt3/prg1p3_3/main.cpp57
1 files changed, 57 insertions, 0 deletions
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 <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;
+} \ No newline at end of file