본문 바로가기

프로그래밍/알고리즘

[Euler Project 101] 수열 다항식으로 근사하기

반응형

#!/usr/bin/env python

# http://projecteuler.net/index.php?section=problems&id=101

coff = [ 0 ] * 11

def OP(ord, n):
       if ord == 0:
               return coff[0]
       r = OP(ord-1, n)
       term = coff[ord]
       for j in range(1, ord+1):
               term*=(n-j)
       for j in range(1, ord+1):
               term/= j
       r += term
       return r

def f(n):
       return 1 - n + n**2 - n**3 + n**4 - n**5 + n**6 - n**7 + n**8 - n**9 + n**10

a = [ f(n) for n in range(1, 12) ]
coff[0] = a[0]
for i in range(1, 11):
       coff[i] = (a[i] - OP(i-1, i+1))
       #print(coff)
#print(a)
sum = 0
for i in range(10):
       op = OP(i, i+2)
       sum += op
       print(i,op, sum)
728x90