Path to mathematics-python computing practice (3)-Horna's Law

Source: Internet
Author: User

Suppose there are n + 2 real numbers a0, a1 ,..., The sequence of an, and x must correspond to the polynomial Pn (x) = anx ^ n + a (n-1) x ^ (n-1) +... + A1x + a0 evaluate the value. The direct method is to evaluate each item separately and accumulate the value of each item. This method is very inefficient and requires n + (n-1) +... + 1 = n (n + 1)/2 multiplication and n addition operations. Is there any more efficient algorithm? The answer is yes. Through the following transformation, we can get a much faster algorithm, that is, Pn (x) = anx ^ n + a (n-1) x ^ (n-1) +... + A1x + a0 = ((... (Anx + an-1) x + an-2) x + an-3 )...) X + a1) x + a0, which is called the Horna rule.

All content of this blog is original, if reproduced please indicate the source

A = [1,-6, 8, 8, 4,-40]

Myx = 3

B = []

B. append (a [0])

For ai in a [1:]:

B. append (ai + myx * B [-1])

Print B [-1]

Deep @ myddb :~ /Src $ pypy test. py

Deep @ myddb :~ /Src $

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.