pol, root = [2,1,-5,2], 1

def horner(pol, root):
    n = len(pol)
    row, ans = [' '], []
    ans.append(pol[0])
    for i in range(1, n):
        s =  root * ans[i - 1]
        row.append(s)
        ans.append(s + pol[i])
    fmt = lambda r : ' '.join(str(x).rjust(3, ' ') for x in r)
    ls = list(map(fmt, (pol, row, ans)))
    k = max(len(l) for l in ls)
    sep =  '\n'+ '_' * (k + 2) + '\n'
    return sep.join(ls)

print(horner(pol, root))
    

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: