Module:Rational: Difference between revisions
mNo edit summary |
convergents() implemented |
||
Line 44: | Line 44: | ||
end | end | ||
return val | return val | ||
end | |||
function p.convergents(x, stop) | |||
local convergents = {} | |||
local data = {} | |||
local i = 0 | |||
while true do | |||
local n = math.floor(x) | |||
table.insert(data, n) | |||
local frac = p.from_continued_fraction(data) | |||
if type(stop) == 'function' and stop(frac) then | |||
break | |||
elseif type(stop) == 'number' and i >= stop then | |||
break | |||
end | |||
table.insert(convergents, frac) | |||
x = x - n | |||
x = 1 / x | |||
i = i + 1 | |||
end | |||
return convergents | |||
end | end | ||