RosettaCodeData/Task/Forward-difference/Pluto/forward-difference.pluto
2026-04-30 12:34:36 -04:00

21 lines
598 B
Text

local fmt = require "fmt"
require "table2"
local function forward_diff(a, order)
assert(order >= 0 and order % 1 == 0, "Order must be a non-negative integer.")
if #a == 0 then return end
io.write(" 0: ")
fmt.tprint("%5d", a)
if #a == 1 then return end
if order == 0 then return end
for o = 1, order do
local b = table.rep(#a - 1, 0)
for i = 1, #b do b[i] = a[i + 1] - a[i] end
io.write($" {o}: ")
fmt.tprint("%5d", b)
if #b == 1 then return end
a = b
end
end
forward_diff({90, 47, 58, 29, 22, 32, 55, 5, 55, 73}, 9)