[Basis of algorithm design and analysis] 18. Horna law, algorithm design and analysis

Source: Internet
Author: User

[Basis of algorithm design and analysis] 18. Horna law, algorithm design and analysis

 

Generate random number

Package cn. xf. algorithm. ch02; import java. util. arrayList; import java. util. list; /*** generate Random number * @ author xiaof **/public class Random {/*** generate a Random number series * @ param n generate n Series * @ param m data in * @ param seed random initial seed * @ M-1 am a parameter * @ param B parameter * @ return */public static List <Integer> randomNum (int n, int m, int seed, int a, int B) {List <Integer> numbers = new ArrayList <Integer> (); int initData = (a * seed + B) % m; numbers. add (Math. abs (initData); // initialize a data for (int I = 1; I <n; ++ I) {int newData = (a * numbers. get (I-1) + B) % m; numbers. add (Math. abs (newData);} return numbers ;} /*** generate a random number series * @ param n generate n Series * @ param m data between m-1 * @ param seed random initial seed * @ param a parameter * @ param B parameter * @ return */public static List <Double> randomNumDouble (int n, int m, int seed, int a, int B) {// create a result array List <Double> numbers = new ArrayList <Double> (); int initData = (a * seed + B) % m; // retrieve an initial value between 0 and m numbers. add (double) Math. abs (initData); // Add the first value // the previous data of the subsequent value is used as the base seed for transformation (int I = 1; I <n; ++ I) {double newData = (a * numbers. get (I-1) + B) % m; numbers. add (Math. abs (newData);} return numbers;} public static void main (String [] args) {// List <Integer> res = Random. randomNum (10, 10,998,587 97676, 1); List <Double> res = Random. randomNumDouble (10, 10,998,587 97676, 1); for (Double a: res) {System. out. print (a + "\ t ");}}}

  

Random value coefficient

 

Evaluate

Package cn. xf. algorithm. ch06ChangeRule; import java. util. arrayList; import java. util. arrays; import java. util. list; import org. junit. test; import cn. xf. algorithm. ch02.Random;/*** function: Horna Rule * @ author xiaofeng * @ date July 22, July 13, 2017 * @ fileName HornerRule. java **/public class HornerRule {/*** calculate the value of a polynomial at a given point using the hoona Law * input: an array of coefficients of n polynomials P [0... n] (from low to high storage) and a number x * output: polynomial value at x points * @ param p * @ param x */public Double horner (List <Double> p, int x) {if (p = null | p. size () <= 0) {return 0d;} // returns the result set Double result = p. get (p. size ()-1); for (int I = p. size ()-2; I> = 0; -- I) {// Add the coefficient data cumulatively. // multiply the coefficient of X from large to small at a time by X, then add the coefficient of the next frequency level, and sum it as the coefficient multiplier of the new next number. result = result * x + p. get (I);} return result;}/*** common Calculation Method * @ param p * @ param x * @ return */public Double notHorner (List <Double> p, int x) {if (p = null | p. size () <= 0) {return 0d;} // p is the coefficient storage list Double result = 0d; // for (int I = 0; I <p. size (); ++ I) {result + = p. get (I) * doublePow (x, I);} return result;} // evaluate n power of x public static Double doublePow (double x, int n) {if (x = 0) return 0d; if (n = 0) return 1d; Double result = 1d; for (int I = 0; I <n; ++ I) {result * = x;} return result ;}@ Testpublic void test1 () {// an array is the coefficient of the equation, the second parameter is the value of the unknown // equation: y = 5x ^ 5 + 3x ^ 4 + 2x ^ 2 + 3 // when x is 4, HornerRule hr = new HornerRule (); list <Double> xishus = new ArrayList <Double> (); // the order of this array is xishus from the power of 0 to the power of N. addAll (Arrays. asList (3d, 0d, 2d, 0d, 3d, 5d); System. out. println (hr. horner (xishus, 4); // calculate System in general mode. out. println (hr. notHorner (xishus, 4); System. out. printf ("job start output: % tF % <tT % n", System. currentTimeMillis ();} @ Test public void compare () {// when x is 4, HornerRule hr = new HornerRule (); // construct a List of 100 Random numbers <Double> xishus = Random. randomNumDouble (600, 3,998,587 97676, 1); // evaluate System. out. printf ("job horner start output: % tF % <tT % n", System. currentTimeMillis (); System. out. println (hr. notHorner (xishus, 3); System. out. printf ("job horner end output: % tF % <tT % n", System. currentTimeMillis (); System. out. println ("##################################### ######################################## #########"); system. out. printf ("job nothorner start output: % tF % <tT % n", System. currentTimeMillis (); System. out. println (hr. notHorner (xishus, 3); System. out. printf ("job nothorner end output: % tF % <tT % n", System. currentTimeMillis ());}}

  

 

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.