The application of power operation, polynomial multiplication and Horner rule

Source: Internet
Author: User

One, two different power-seeking operations

Solving x^n (n-th-square of X)

① uses recursion, the code is as follows:

1     Private Static LongPowintXintN) {2         if(n = = 0)3             return1;4         if(n = = 1)5             returnx;6         if(n% 2 = = 0)7             returnPOW (x * x, N/2);8         Else9             returnPOW (x * x, N/2) *x;Ten}

Analysis:

Each recursive result in halving the size of the problem. 2 to 6 row operation complexity is O (1), 7th Line POW function inside the x*x operation complexity is O (1)

The time complexity formula: T (n) =t (N/2) +o (1) = = T (n) =o (LOGN)

② Common Way exponentiation

1     Private Static LongPow2 (intXintN) {2         if(x = = 0)3             return0;//0^n = = 04         Longp = 1;5          for(inti = 0; I < n; i++)6P *=x;7         returnp;8}

Obviously, the time complexity is O (N)

Second, solving polynomial multiplication

Formula: F (x,n) = A (0) x^0 + A (1) x^1 + A (2) x^2+...+a (n) x^n

For example: F (10,4) =a (0) 10^0 + A (1) 10^1 + A (2) 10^2 + A (3) 10^3+a (4) 10^4

The code is as follows:

1      Public Static LongPolyint[] arr,intXintN) {2         Longsum = 0;3          for(inti = 0; I <= N; i++){4Sum + = arr[i] *Pow (x, i);5         }6         returnsum;7     }8 9     Private Static LongPowintXintN) {Ten         if(n = = 0) One             return1; A         if(n = = 1) -             returnx; -         if(n% 2 = = 0) the             returnPOW (x * x, N/2); -         Else -             returnPOW (x * x, N/2) *x; -}

The Horner law solves polynomial multiplication by reference to:

 1  public  static  long  poly2 (int  [] arr, Span style= "color: #0000ff;" >int  x, int   N) {//arr storage factor, X for cardinality, N for power  2  long  poly = 0; 3   int  i = n; I >= 0; I--)  4  poly = Poly * x + Arr[i];  5  return   Poly;  6 } 

Using the Horner rule to calculate polynomial multiplication with this article: string conversion to Digital

1  Public int atoi (char[] s) {2         int result = 0; 3          for (int i = 0; i < s.length; i++) 4             Result = result * + s[i]-' 0 ';//equivalent to Poly2 (...) x=10 5 in the         return  result; 6     }

It can be seen that there is a great similarity between the two. In fact, it is not difficult to see that the string converted into a digital use is exactly the Horner law.

From this, it is enlightened that in the binary conversion, such as: octal decimal, equivalent to x = 8. hexadecimal to decimal, equivalent to x = 16.

Therefore, a commonly used conversion program can be written as follows:

    //x indicates the binary, if x=8, means converting 8 binary to 10 binary     Public Static LongConvertChar[] arr,intx) {        Longresult = 0;  for(inti = 0; i < arr.length; i++) Result= result * x + arr[i]-' 0 '; returnresult; }        //STR Indicates the number of original binary, such as convert ("456", 8) 456 --302     Public Static LongConvert2 (String str,intx) {        Longresult = 0;  for(inti = 0; I < str.length (); i++) Result= result * x + integer.valueof (Str.charat (i)-' 0 '); returnresult; }

Therefore, the Horner law can be used to solve the binary conversion, the conversion of strings into numbers, and the evaluation of polynomial values.

The application of power operation, polynomial multiplication and Horner rule

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: info-contact@alibabacloud.com 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.