Hong-Phuc Bui
2024-04-29 931d213d4395c6d0ef93f30fe606da8ee3baa6d2
update permutation
2 files modified
9 files added
163 ■■■■■ changed files
python-c-interface/prime-factory.py patch | view | raw | blame | history
python-grundlage/binary.py 22 ●●●●● patch | view | raw | blame | history
python-grundlage/cli-example.py 7 ●●●●● patch | view | raw | blame | history
python-grundlage/euclid-gcd.py 13 ●●●●● patch | view | raw | blame | history
python-grundlage/find-machinell-epsilon.py 18 ●●●●● patch | view | raw | blame | history
python-grundlage/harmonic-serie.py 9 ●●●●● patch | view | raw | blame | history
python-grundlage/hex-table.py 10 ●●●●● patch | view | raw | blame | history
python-grundlage/permutation-iterative.py 31 ●●●●● patch | view | raw | blame | history
python-grundlage/prime-factorize-half.py 21 ●●●●● patch | view | raw | blame | history
python-grundlage/prime-factorize.py 20 ●●●●● patch | view | raw | blame | history
python-grundlage/sqrt-newton.py 12 ●●●●● patch | view | raw | blame | history
python-c-interface/prime-factory.py
python-grundlage/binary.py
New file
@@ -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)
python-grundlage/cli-example.py
New file
@@ -0,0 +1,7 @@
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
s = a + b
print(s)
python-grundlage/euclid-gcd.py
New file
@@ -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)
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}")
python-grundlage/harmonic-serie.py
New file
@@ -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
python-grundlage/hex-table.py
New file
@@ -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()
python-grundlage/permutation-iterative.py
New file
@@ -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)
python-grundlage/prime-factorize-half.py
New file
@@ -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}")
python-grundlage/prime-factorize.py
New file
@@ -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}")
python-grundlage/sqrt-newton.py
old mode 100644 new mode 100755
@@ -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}")