Sagemath-övningar på kedjebråk

Kedjebråk

Ändliga kedjebråk

# Kedjebråksutveckling av rationellt tal
r=1347
s=7341
print(r/s)
kb1 = continued_fraction(r/s)
kb1
show(kb1)
kb1.quotients()
kb1.convergents()
list_plot(kb1.convergents()[1:])
kedjebraklab_fig_1.png
kb2 = continued_fraction([-6,7,8,9,114,137,2])
show(kb2)
kb2.quotients()
kb2.convergents()
kb2.value()

Symboliska kvoter, Eulers regel

def KB(L):

    n = len(L)
  #  print(L,n)
    if n == 0:
        return []
    elif n == 1:
        return L
    elif n == 2:
        return [(L[0]*L[1] +1)/L[1]]
    else:
        FL = L[:-2]
        LL = L[-2:]
        EL = KB(LL)
      #  print(f'EL={EL}')
        return FL + EL

def KBfromback(L):
    LC = L.copy()
    LU=[]
    while (len(LC) >1):
        LC = KB(LC)
        LU = LU + [LC]
    return LU
L1 = [3,4,5,2]
KBfromback(L1)
continued_fraction(L1)
show(continued_fraction(L1))
continued_fraction(L1).value()
# symboliska argument
xl =list(var('x',n=5))
KBfromback(xl)
ut=KBfromback(xl)[-1:][0][0]
ut
ut.expand()
ut.numerator()
ut.denominator()
legendre_symbol(15,101)
continued_fraction(sqrt(2))
for d in range(2,30):
    print(d,continued_fraction(sqrt(d)))
    print