Module:MOS: Difference between revisions

Sintel (talk | contribs)
use raw gcd instead
Inthar (talk | contribs)
m Replacing the algorithm in `brightest_mode()` with the "Christoffel word walk" algorithm which is always linear time.
Line 140: Line 140:
ns = round(ns/d)
ns = round(ns/d)
end
end
local current_nL, current_ns = nL, ns
local current_L, current_s = 0, 0
local path_to_root = {}
local result = ''
while current_nL > 1 or current_ns > 1 do -- while current mos is not 1L 1s<equave>, record current mos and go up to parent
while current_L < a or current_s < b do
table.insert(path_to_root, {[1] = current_nL, [2] = current_ns})
if (current_s + 1) * a <= b * (current_L) then
current_nL, current_ns = math.min(current_nL, current_ns), math.abs(current_nL - current_ns) -- parent's nL and ns
            current_s = current_s + 1
end
            result = result .. 's'
local len_of_path = table.getn(path_to_root)
        else
local result = 'Ls'
            current_L = current_L + 1
for j = len_of_path, 1, -1 do
            result = result .. 'L'
local child_nL, child_ns = path_to_root[j][1], path_to_root[j][2]
        end
if child_nL > child_ns then -- this will give darkest mode, reverse for brightest mode
result = string.gsub(result, '[Ls]', {['L'] = 'sL', ['s'] = 'L'})
result = string.reverse(result)
else
result = string.gsub(result, '[Ls]', {['L'] = 'Ls', ['s'] = 's'})
end
end
end
return string.rep(result, d)
return string.rep(result, d)