Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 5.5 #4

Open
rswinkle opened this issue May 13, 2023 · 0 comments
Open

Exercise 5.5 #4

rswinkle opened this issue May 13, 2023 · 0 comments

Comments

@rswinkle
Copy link

rswinkle commented May 13, 2023

-- Sure, if this is what you mean.

function polyval (p, x)
   local sum, pow = 0, 1
   for _, a in ipairs (p) do
      sum = sum + a * pow
      pow = pow * x
   end
   return sum
end

That would still be 2(n+1) multiplications. They mean this:
a0 + x*(a1 + x*(a2 + x*(a3 ...+ x*an)))))

which would look something like this:

function poly_eval2(p, x)
  ret = p[#p]
  for k = #p-1,1,-1 do
    ret = ret * x
    ret = ret + p[k]
  end
  return ret
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant