From 931d213d4395c6d0ef93f30fe606da8ee3baa6d2 Mon Sep 17 00:00:00 2001
From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de>
Date: Mon, 29 Apr 2024 02:14:56 +0200
Subject: [PATCH] update permutation

---
 python-grundlage/cli-example.py            |    7 ++
 python-grundlage/find-machinell-epsilon.py |   18 ++++--
 python-grundlage/binary.py                 |   22 +++++++
 python-grundlage/sqrt-newton.py            |   12 ++-
 python-grundlage/euclid-gcd.py             |   13 ++++
 python-grundlage/hex-table.py              |   10 +++
 python-grundlage/prime-factorize.py        |   20 ++++++
 python-grundlage/permutation-iterative.py  |   31 ++++++++++
 python-grundlage/harmonic-serie.py         |    9 +++
 python-grundlage/prime-factorize-half.py   |   21 +++++++
 python-c-interface/prime-factory.py        |    0 
 11 files changed, 152 insertions(+), 11 deletions(-)

diff --git a/python-c-interface/prime-factory.py b/python-c-interface/prime-factory.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/python-c-interface/prime-factory.py
diff --git a/python-grundlage/binary.py b/python-grundlage/binary.py
new file mode 100755
index 0000000..6e23ffa
--- /dev/null
+++ b/python-grundlage/binary.py
@@ -0,0 +1,22 @@
+#! /usr/bin/env python
+
+from typing import Final
+import sys
+
+n = int(sys.argv[1])
+power = 1
+LIMIT: Final[int] = n // 2
+
+# find the largest power of 2, which is less than or equal to n/2
+while power <= LIMIT:
+    power *= 2
+
+binary_presentation = ""
+while power > 0:
+    if n < power:
+        binary_presentation += "0"
+    else:
+        binary_presentation += "1"
+        n -= power
+    power //= 2
+print(binary_presentation)
diff --git a/python-grundlage/cli-example.py b/python-grundlage/cli-example.py
new file mode 100644
index 0000000..f64c442
--- /dev/null
+++ b/python-grundlage/cli-example.py
@@ -0,0 +1,7 @@
+import sys
+
+a = int(sys.argv[1])
+b = int(sys.argv[2])
+s = a + b
+
+print(s)
\ No newline at end of file
diff --git a/python-grundlage/euclid-gcd.py b/python-grundlage/euclid-gcd.py
new file mode 100644
index 0000000..41ffcac
--- /dev/null
+++ b/python-grundlage/euclid-gcd.py
@@ -0,0 +1,13 @@
+#! /usr/bin/env python
+
+import sys
+
+a = int(sys.argv[1])
+b = int(sys.argv[2])
+
+while b != 0:
+    t = b
+    b = a % b
+    a = t
+
+print(a)
diff --git a/python-grundlage/find-machinell-epsilon.py b/python-grundlage/find-machinell-epsilon.py
index 4fd47a5..9505c55 100644
--- a/python-grundlage/find-machinell-epsilon.py
+++ b/python-grundlage/find-machinell-epsilon.py
@@ -1,13 +1,19 @@
-# Credit: https://stackoverflow.com/a/57579379
-
 import sys
 
-eps = 1.0
-while eps + 1 > 1:
-    eps /= 2
-eps *= 2
 sys_eps = sys.float_info.epsilon
+
+eps = 1.0
+while 1.0 + eps > 1.0:
+    eps /= 2.0
+eps *= 2.0
+
 print(f"The calculated epsilon: {eps}")
 print(f"    The system epsilon: {sys_eps}")
 
+# with assign expression
+eps = 1.0
+while 1.0 + (eps := eps / 2.0) > 1.0: pass
+eps *= 2.0
 
+print(f"The calculated epsilon: {eps}")
+print(f"    The system epsilon: {sys_eps}")
diff --git a/python-grundlage/harmonic-serie.py b/python-grundlage/harmonic-serie.py
new file mode 100644
index 0000000..d9a42e3
--- /dev/null
+++ b/python-grundlage/harmonic-serie.py
@@ -0,0 +1,9 @@
+n = 10
+h = 1
+algebraic_h = "1"
+
+for k in range(2, n+2):
+    print(f"{algebraic_h} = {h}")
+    algebraic_h = f"{algebraic_h} + 1/{k}"
+    h = h + 1/k
+
diff --git a/python-grundlage/hex-table.py b/python-grundlage/hex-table.py
new file mode 100755
index 0000000..fee668d
--- /dev/null
+++ b/python-grundlage/hex-table.py
@@ -0,0 +1,10 @@
+#! /usr/bin/env python
+# -*-encoding utf-8-*-
+
+n = 0xF
+
+for i in range(n + 1):
+    for j in range(n + 1):
+        p = i * j
+        print(f"{p:02X} ", end="")
+    print()
diff --git a/python-grundlage/permutation-iterative.py b/python-grundlage/permutation-iterative.py
new file mode 100755
index 0000000..8b4030c
--- /dev/null
+++ b/python-grundlage/permutation-iterative.py
@@ -0,0 +1,31 @@
+#! /usr/bin/env python
+import sys
+
+n = int(sys.argv[1])
+
+# permutations = [[0]]
+# element = 1
+# n = 2
+#
+# next_permutations = []
+# for p in permutations:
+#     for position in range(0, len(p) + 1):
+#         new_permutation = p.copy()
+#         new_permutation.insert(position, element)
+#         next_permutations.append(new_permutation)
+# print(next_permutations)
+
+permutations = [[]]
+element = n - 1
+
+while element >= 0:
+    next_permutations = []
+    for p in permutations:
+        for position in range(len(p), -1, -1):
+            new_permutation = p.copy()
+            new_permutation.insert(position, element)
+            next_permutations.append(new_permutation)
+    permutations = next_permutations
+    element -= 1
+
+print(permutations)
\ No newline at end of file
diff --git a/python-grundlage/prime-factorize-half.py b/python-grundlage/prime-factorize-half.py
new file mode 100755
index 0000000..d309f51
--- /dev/null
+++ b/python-grundlage/prime-factorize-half.py
@@ -0,0 +1,21 @@
+#! /usr/bin/env python
+import sys
+n = int(sys.argv[1])
+
+# quote_test ist ein 2-er Tupel (q, r)
+while (quote_rest := divmod(n, 2)) and quote_rest[1] == 0:
+    print(f"{2} ", end="")
+    n = quote_rest[0]
+
+p = 3
+while p * p <= n:
+    if (quote_rest := divmod(n, p)) and quote_rest[1] == 0:
+        print(f"{p} ", end="")
+        n = quote_rest[0]
+    else:
+        p += 2
+if n != 1:
+    print(n)
+
+#print(f"iteration: {__iteration}")
+
diff --git a/python-grundlage/prime-factorize.py b/python-grundlage/prime-factorize.py
new file mode 100755
index 0000000..15cfe8e
--- /dev/null
+++ b/python-grundlage/prime-factorize.py
@@ -0,0 +1,20 @@
+#! /usr/bin/env python
+# -*-encoding utf-8-*-
+
+import sys
+
+n = int(sys.argv[1])
+p = 2
+
+iteration = 0
+while n > 1:
+    q, r = divmod(n, p)
+    if r == 0:
+        print(f"{p} ", end="")
+        n = q
+    else:
+        p += 1
+    iteration += 1
+
+print(f"\niteration: {iteration}")
+
diff --git a/python-grundlage/sqrt-newton.py b/python-grundlage/sqrt-newton.py
old mode 100644
new mode 100755
index 00cfa07..3eb8e68
--- a/python-grundlage/sqrt-newton.py
+++ b/python-grundlage/sqrt-newton.py
@@ -1,10 +1,12 @@
+#! /usr/bin/env python
 import sys
+from typing import Final
 
 n = float(sys.argv[1])
-EPSILON = sys.float_info.epsilon
+EPSILON: Final[float] = sys.float_info.epsilon
 
-t = n
-while abs(t - n/t) > (EPSILON*t):
-    t = (n / t + t) / 2
+x = n                                         # !$x_0 = n$!
+while abs(x - (q := n / x)) > (EPSILON * x):  # !$\left|\frac{x^2-n}{x^2}\right| > \varepsilon $!
+    x = (x + q) / 2.0                         # !$x_{i+1} = \frac{1}{2}(x_i + \frac{n}{x_i})$!
 
-print(f"sqrt({n}) ~= {t}")
+print(f"sqrt({n}) ~ {x}")

--
Gitblit v1.10.0-SNAPSHOT