For an algorithm with a running time of N * n, the algorithm with a running time of 2 ^ n runs very fast on the same machine. What is the minimum value of n?

Source: Internet
Author: User

In 《AlgorithmIn the first part of the introduction, there is an algorithm question:

 
1.2-3 for an algorithm with a running time of N x N, the algorithm with a running time of 2 ^ n needs to run quickly on the same machine, what is the minimum value of n?

The following provides my own solutions:

 
For the comparison between 100n ^ 2 and 2 ^ n algorithms, we can do this: for the 100n ^ 2-2 ^ n operation, if the result is less than 0, then, n is the value we want.

The following algorithm implementation is provided for this idea:

 1   /**  2   *  3    */  4   Package  Com. b510.algorithms;  5  6   /**  7   * Introduction to algorithms Part 1: Exercise 1.2-3: For an algorithm with a running time of 100n ^ 2, make it on the same machine, returns the result of a 2 ^ n running time.  8   * The method runs faster. What is the minimum value of n?  9   *  10   *  @ Author  Hong (hongtenzone@foxmail.com) <br>  11   * @ Date 2013-6-6  12    */ 13   Public   Class  Partoneofalgorithms {  14   15       Public   Static   Void  Main (string [] ARGs ){  16   Getsum ();  17   }  18   19       /** 20   * To compare the 100n ^ 2 and 2 ^ n algorithms, we can perform the 100n ^ 2-2 ^ n operation. If the result is smaller than 0, then, n is the value we want.  21   * In Java, evaluate the N power of a number by using math. Pow (x, y), that is, the y power of X.  22        */  23       Public   Static   Void  Getsum (){  24           Int N = 1 ;  25          Long Sum = 0 ;  26           Boolean Flag = True  ;  27           While  (FLAG ){  28 Sum = ( Long ) (100 * (math. Pow (n, 2)-math. Pow (2 , N ));  29 System. Out. println ("nth" + N + "Calculation Result:" + Sum ); 30               If (Sum <0 ){  31 Flag = False  ;  32                   Break  ;  33   }  34 N = n + 1 ;  35   }  36  System. Out. println (N );  37   }  38 }

Running effect:

1st calculation results: 98The 2nd calculation result is:396The 3rd calculation result is:892The 4th calculation result is:1584The 5th calculation result is:2468The 6th calculation result is:3536The 7th calculation result is:4772The 8th calculation result is:6144The 9th calculation result is:7588The 10th calculation result is:8976The 11th calculation result is:10052The 12th calculation result is:10304The 13th calculation result is:8708The 14th calculation result is:3216The 15th calculation result is:-1026815

 

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: 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.