@@ 21,18 21,19 @@ In general
P(-1) = sum i ∈ [0, n - 1] of (-1)^i * binom(n, i + 1) * value[i]
--]]
-local line, sign, coeffs, ans1, ans2 = io.read(), -1, {1}, 0, 0
+local line, coeffs = io.read(), {-1}
for _ in line:gmatch('-?%d+') do
+ coeffs[#coeffs + 1] = 0
for i = #coeffs, 2, -1 do
coeffs[i] = coeffs[i] - coeffs[i - 1]
end
- coeffs[#coeffs + 1], sign = sign, -sign
end
+local ans1, ans2 = 0, 0
repeat
local i = 1
for value in line:gmatch('-?%d+') do
- ans1, i = ans1 + value * coeffs[i] * sign, i + 1
- ans2 = ans2 - value * coeffs[i]
+ ans1, i = ans1 + value * coeffs[#coeffs - i + 1], i + 1
+ ans2 = ans2 + value * coeffs[i]
end
line = io.read()
until not line