Goodstein Sequences in Python

As the CDF file from my video can't be viewed in current browsers anymore and the CDF Player doesn't seem to be used a lot anyway, I have programmed the generation of Goodstein sequences once again, this time in Python. The HTML was generated in Jupyter.

In [1]:
def normalForm (x, b):
    if x < b:
        return x
    L = []
    e = 0
    while x > 0:
        m = x % b
        if m > 0:
            L.append([normalForm(e, b), m])
        x //= b
        e += 1
    return L

def fromNormalForm (L, b):
    if type(L) is int:
        return L
    return sum(f * b**fromNormalForm(e, b) for e, f in L)
In [2]:
from re import match

def maybeAddParens (s):
    return s if match(r"^[0-9]+$", s) else "(" + s +")"

def beautify (f, b, e):
    if e == 0:
        return str(f)
    if e == 1:
        if f == 1:
            return str(b)
        else:
            return "{}*{}".format(f, b)
    if f == 1:
        return "{}^{}".format(b, maybeAddParens(normalFormToString(e, b)))
    return "{}*{}^{}".format(f, b, maybeAddParens(normalFormToString(e, b)))

def normalFormToString (L, b):
    if type(L) is int:
        return str(L)
    return "+".join(beautify(f, b, e) for e, f in L)
In [3]:
def goodstein (x, n):
    b = 2
    while b <= n + 1:
        if x == 0:
            break
        N = normalForm(x, b)
        print("{} = {}".format(x, normalFormToString(N, b)))
        b += 1
        x = fromNormalForm(N, b) - 1
In [4]:
goodstein(13, 7)
13 = 1+2^2+2^(1+2)
108 = 3^3+3^(1+3)
1279 = 3+3*4+3*4^2+3*4^3+4^(1+4)
16092 = 2+3*5+3*5^2+3*5^3+5^(1+5)
280711 = 1+3*6+3*6^2+3*6^3+6^(1+6)
5765998 = 3*7+3*7^2+3*7^3+7^(1+7)
134219479 = 7+2*8+3*8^2+3*8^3+8^(1+8)
In [5]:
goodstein(7, 13)
7 = 1+2+2^2
30 = 3+3^3
259 = 3+4^4
3127 = 2+5^5
46657 = 1+6^6
823543 = 7^7
16777215 = 7+7*8+7*8^2+7*8^3+7*8^4+7*8^5+7*8^6+7*8^7
37665879 = 6+7*9+7*9^2+7*9^3+7*9^4+7*9^5+7*9^6+7*9^7
77777775 = 5+7*10+7*10^2+7*10^3+7*10^4+7*10^5+7*10^6+7*10^7
150051213 = 4+7*11+7*11^2+7*11^3+7*11^4+7*11^5+7*11^6+7*11^7
273624711 = 3+7*12+7*12^2+7*12^3+7*12^4+7*12^5+7*12^6+7*12^7
475842915 = 2+7*13+7*13^2+7*13^3+7*13^4+7*13^5+7*13^6+7*13^7
794655639 = 1+7*14+7*14^2+7*14^3+7*14^4+7*14^5+7*14^6+7*14^7

 

Impressum, Datenschutzerklärung