Kedjebråk
Ändliga kedjebråk
# Kedjebråksutveckling av rationellt tal
r=1347
s=7341
print(r/s)
kb1 = continued_fraction(r/s)
kb1
449/2447
[0; 5, 2, 4, 2, 22]
Messages
Messages
show(kb1)
\[\newcommand{\Bold}[1]{\mathbf{#1}}0
+ \frac{\displaystyle 1}{\displaystyle 5
+ \frac{\displaystyle 1}{\displaystyle 2
+ \frac{\displaystyle 1}{\displaystyle 4
+ \frac{\displaystyle 1}{\displaystyle 2
+ \frac{\displaystyle 1}{\displaystyle 22
}}}}}\]
Messages
kb1.quotients()
[0, 5, 2, 4, 2, 22]
Messages
kb1.convergents()
[0, 1/5, 2/11, 9/49, 20/109, 449/2447]
Messages
list_plot(kb1.convergents()[1:])
Messages
Messages
kb2 = continued_fraction([-6,7,8,9,114,137,2])
show(kb2)
\[\newcommand{\Bold}[1]{\mathbf{#1}}-6
+ \frac{\displaystyle 1}{\displaystyle 7
+ \frac{\displaystyle 1}{\displaystyle 8
+ \frac{\displaystyle 1}{\displaystyle 9
+ \frac{\displaystyle 1}{\displaystyle 114
+ \frac{\displaystyle 1}{\displaystyle 137
+ \frac{\displaystyle 1}{\displaystyle 2
}}}}}}\]
Messages
kb2.quotients()
[-6, 7, 8, 9, 114, 137, 2]
Messages
kb2.convergents()
[-6, -41/7, -334/57, -3047/520, -347692/59337, -47636851/8129689, -95621394/16318715]
Messages
kb2.value()
-95621394/16318715
Messages
Messages
Messages
Messages
Messages
Messages
Messages
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
Messages
def KBfromback(L):
LC = L.copy()
LU=[]
while (len(LC) >1):
LC = KB(LC)
LU = LU + [LC]
return LU
Messages
L1 = [3,4,5,2]
KBfromback(L1)
[[3, 4, 11/2], [3, 46/11], [149/46]]
Messages
continued_fraction(L1)
[3; 4, 5, 2]
Messages
continued_fraction(L1).value()
149/46
Messages
# symboliska argument
xl =list(var('x',n=5))
KBfromback(xl)
[[x0, x1, x2, (x3*x4 + 1)/x4], [x0, x1, ((x3*x4 + 1)*x2/x4 + 1)*x4/(x3*x4 + 1)], [x0, (x3*x4 + 1)*(x1*((x3*x4 + 1)*x2/x4 + 1)*x4/(x3*x4 + 1) + 1)/(((x3*x4 + 1)*x2/x4 + 1)*x4)], [((x3*x4 + 1)*x2/x4 + 1)*((x3*x4 + 1)*(x1*((x3*x4 + 1)*x2/x4 + 1)*x4/(x3*x4 + 1) + 1)*x0/(((x3*x4 + 1)*x2/x4 + 1)*x4) + 1)*x4/((x3*x4 + 1)*(x1*((x3*x4 + 1)*x2/x4 + 1)*x4/(x3*x4 + 1) + 1))]]
Messages
ut=KBfromback(xl)[-1:][0][0]
ut
((x3*x4 + 1)*x2/x4 + 1)*((x3*x4 + 1)*(x1*((x3*x4 + 1)*x2/x4 + 1)*x4/(x3*x4 + 1) + 1)*x0/(((x3*x4 + 1)*x2/x4 + 1)*x4) + 1)*x4/((x3*x4 + 1)*(x1*((x3*x4 + 1)*x2/x4 + 1)*x4/(x3*x4 + 1) + 1))
Messages
ut.expand()
x0*x1*x2^2*x3^3*x4^2/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x2*x3 + x2/x4 + 1)*(x3*x4 + 1)^2) + 3*x0*x1*x2^2*x3^2*x4/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x2*x3 + x2/x4 + 1)*(x3*x4 + 1)^2) + 2*x0*x1*x2*x3^2*x4^2/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x2*x3 + x2/x4 + 1)*(x3*x4 + 1)^2) + x0*x2*x3^2*x4/((x1*x2*x3*x4/(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 + 1)^2*x4) + x4/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x3*x4 + 1)) + x0*x1*x4/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x2*x3 + x2/x4 + 1)*(x3*x4 + 1)^2) + x0/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x2*x3 + x2/x4 + 1)*(x3*x4 + 1)) + x0*x2/((x1*x2*x3*x4/(x3*x4 + 1) + x1*x2/(x3*x4 + 1) + x1*x4/(x3*x4 + 1) + 1)*(x2*x3 + x2/x4 + 1)*(x3*x4 + 1)*x4)
Messages
ut.numerator()
x0*x1*x2*x3*x4 + x0*x1*x2 + x0*x1*x4 + x0*x3*x4 + x2*x3*x4 + x0 + x2 + x4
Messages
ut.denominator()
x1*x2*x3*x4 + x1*x2 + x1*x4 + x3*x4 + 1
Messages
Messages