Search code examples
algorithmimplementationpseudocodepolynomials

How do I put this algorithm in programming languages (Python)


Here is an algorithm on evaluating polynomials

//input---->p- polynomial
            n- degree of p
            a- the value at which to evaluate p(x)
output: p(a)//
function evaluate(p,n,a)
begin
 value=0.0;
for i:=0 to n do
 if p[i]!= 0.0 then
   value=value+p[i]*a^i;
 return value;
end

How do I code this algorithm in a programming language (Python)?


Solution

  • In Python it's

    def evaluate(p,n,a):
        value=0.0
        for i in range (0, n):
            if p[i]!= 0.0:
                value=value+p[i]*a**i
        return value;