Search code examples
algorithmbig-opseudocode

Arithmetic Algorithm


I was going through some interview question and stumbled upon this question. p(x) = a0 + a1x + a2x^2 + ... + anx^n. What algorithm could you use to to compute the value of p(x) in O(N^2) ? I'm totally clueless about how to approach this problem.


Solution

  • You can do it in O(N) with direct evaluation or with Horner's method. A useful chart for complexity of various operations and the methods involved can be found here:

    Computational complexity of mathematical operations

    Horner's method is a serial procedure that optimizes "sub-expressions of form (A+ Bx) which be evaluated using a native multiply–accumulate instruction on some architectures". A more parallel version is Estrin's scheme.

    Since you can compute the p(x) in O(N) time, apply this method N times to achieve O(N^2) (if that is really what you are after...).