project-euler/101_optimum_polynomials.py

28 lines
576 B
Python
Raw Normal View History

2023-03-26 17:56:21 +02:00
#!/usr/bin/env python3
CFS = [1, -1]*5 + [1]
def main(cfs):
seq = getseq(cfs)
py = pyramid(seq)
return sum(op(n, py) for n in range(1, len(cfs)))
def getseq(cfs):
return [sum(c*n**pw for pw, c in enumerate(cfs))
for n in range(1, len(cfs))]
def pyramid(seq):
r = [seq]
for i in range(len(seq)-1):
lvl = []
for j in range(len(r[i])-1):
lvl.append(r[i][j+1] - r[i][j])
r.append(lvl)
return r
def op(n, py):
return sum(py[i][n-i-1] for i in range(n))
#print(main([0,0,0,1]))
print(main(CFS))