local fmt = require "fmt" local function sma(period) local i = 0 local sum = 0 local storage = {} return function(input) if #storage < period then sum += input storage:insert(input) end sum += input - storage[i + 1] storage[i + 1] = input i = (i + 1) % period return sum / #storage end end local sma3 = sma(3) local sma5 = sma(5) print(" x sma3 sma5") for {1, 2, 3, 4, 5, 5, 4, 3, 2, 1} as x do fmt.print("%5.3f %5.3f %5.3f", x, sma3(x), sma5(x)) end