"Fundamentals of Algorithmic Design and analysis" 18, Horner's Law

Source: Internet
Author: User
Tags abs

Generate random numbers

Package Cn.xf.algorithm.ch02;import Java.util.arraylist;import java.util.list;/** * Production random number * @author xiaof * */public clas s random {/** * produces a sequence of random numbers * @param n generates n columns * @param m data between 0 and m-1 * @param seed random Initial seed * @param a parameter * @param b parameter * @re  Turn */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 &l T N ++i) {int NewData = (A * Numbers.get (i-1) + b)% M;numbers.add (Math.Abs (NewData));} return numbers;}     /** * Produces a sequence of random numbers * @param n generates n columns * @param m data between 0 and 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 result array    list<double> numbers = new arraylist<double> (); int InitData = (A * seed + b)% m; Take out an initial value between 0 and M Numbers.add ((double) Math.Abs (InitData));   Add the first value//subsequent value before a data is transformed as the underlying seed for (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, 58797676, 1);  list<double> res = random.randomnumdouble (ten, 998, 58797676, 1); for (Double a:res) {System.out.print (a + "\ T");} }}

  

Random coefficient of value

Value evaluation

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: Horner's Law * @author Xiaofeng * @date July 13, 2017 * @fileNam E Hornerrule.java * */public class Hornerrule {/** * Use Horner's law to find a polynomial at a given point value * Input: an n-th polynomial coefficient array P "0...N" (from low to high storage), and a number x * Output: multiple  The value of the formula at x Point * @param p * @param x */public Double Horner (list<double> p, int x) {if (P = = NULL | | p.size () <=0) {return 0d;} Result set Double result = P.get (P.size ()-1); for (int i = P.size ()-2; I >= 0; i) {//accumulate to add coefficient data//one time from large to small bar x coefficient multiplied by x, then add next The coefficients of the number of times rank, and then sum, as a new multiplier for the next number of times result = result * x + p.get (i);} return result;} /** * Normal 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 store list Double result = 0d;    For the power of 0 (int i = 0; i < p.size (); ++i) {result + = P.get (i) * DOUBLEPOW (x, i); } rEturn result;}        The n power of X is the 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 () {///define 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 to be Xishus.addall (Arrays.aslist (3d, 0d, 2d, 0d, 3d, 5d) in the order of 0 power to n Power);    System.out.println (Hr.horner (Xishus, 4));    The general method calculates the System.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 ();        Build 100 random numbers list<double> Xishus = random.randomnumdouble (600, 3, 998, 58797676, 1); Evaluation 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 ()); }}

  

"Fundamentals of Algorithmic Design and analysis" 18, Horner's Law

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.