Continued Fraction Approximations of a Square Root
Posted: Sat Apr 04, 2009 3:09 pm
may not be the best running time but
Code: Select all
If OpenConsole()
Repeat
Repeat
Print("Number:")
s.l = Val(Input())
Until s <> 0
Repeat
Print("Length:")
l.l = Val(Input())
Until l <> 0
PrintN("")
m.l = 0
d.l = 1
a.l = Int(Sqr(s))
hm2.q = 0
hm1.q = 1
h.q = 0
km2.q = 1
km1.q = 0
k.q = 0
For i = 1 To l
If i <> 1
Print(",")
EndIf
Print(Str(a))
m = d*a-m
d = (s-m*m)/d
a = Int((Sqr(s)+m)/d)
Next
PrintN("")
PrintN("")
m.l = 0
d.l = 1
a.l = Int(Sqr(s))
hm2.q = 0
hm1.q = 1
h.q = 0
km2.q = 1
km1.q = 0
k.q = 0
Print("Convergents:")
For i = 1 To l
If i <> 1
Print(",")
EndIf
h = a*hm1+hm2
k = a*km1+km2
Print(Str(h)+"/"+Str(k))
m = d*a-m
d = (s-m*m)/d
a = Int((Sqr(s)+m)/d)
hm2 = hm1
hm1 = h
km2 = km1
km1 = k
Next
PrintN("")
PrintN("")
ForEver
EndIf