25 lines
596 B
Python

from functools import reduce
def poly(x, l):
return reduce(lambda s, a: x * (s + a), l[:-1], 0) + l[-1]
def gen_item(l):
for i in range(1, len(l)):
yield poly(i, l)
def gen_lagrange(n, k):
lst = list(filter(lambda x: x != k, range(n)))
tale = 1
for x in lst:
tale *= n - x
for x in lst:
tale //= k - x
return tale
def calc(l):
lst = list(gen_item(l))
for n in range(1, len(l)):
yield sum(map(lambda x: x[0] * x[1], zip(lst, [gen_lagrange(n, k) for k in range(n)])))
print(sum(calc([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1])))