Classic algorithm Detailed (9) Looking for ugly number

Source: Internet
Author: User

Title: We call only the number of factors 2, 3, 5 is called the ugly number. For example, 6, 8 are ugly number, and 14 is not ugly number, because it contains factor 7. Usually 1 as an ugly number. Programming to find all the ugly numbers within 1500. Note: The efficiency of the algorithm used should be as high as possible.

C + + implementation:

1#include <iostream>2 3 using namespacestd;4 5 //judge whether a number is an ugly number, uglynum=2*2......*2*3*3*......*3*5*5......*5 composition6 intIsuglynumber (intN) {7      while(n%2==0)8     {9N/=2;Ten     } One      while(n%3==0) A     { -N/=3; -     } the      while(n%5==0) -     { -N/=5; -     } +     returnn = =1; - } + //Method 1: Judge whether it is ugly uncle, the idea is simple, but computational redundancy, because the more to the back many are not ugly number is also in the calculation.  A intGet_ugly_fir (intNumber ) { at     inti =1; -     intCount =0; -      while(i<=Number ) -     { -         if(Isuglynumber (i)) { -cout << i <<"\ t"; incount++; -             if(Count%Ten==0) tocout <<Endl; +         } -i++; the     } *     returncount; $ }Panax Notoginseng  - //method Two: High efficiency of the algorithm.  the //idea: (1) The ugly number behind must be the existing ugly number multiplied by 2, 3 or 5, find the ugly number is larger than the present and is the smallest ugly number as the next ugly number (how to find is the key).  + //2 respectively from the existing ugly number from the previous times the number of ugly, find the first greater than the current number of all the ugly value and position, 3, 5 the same, and then multiply their results after the comparison, take the smallest.  A //The next time you will start looking down from the last position, this will not be redundant, see the following function in detail.  the  + intnext_ugly (intUgly_arr[],int*LOC2,int*LOC3,int*LOC5,int*index) { -      while(UGLY_ARR[*LOC2] *2<= Ugly_arr[*index]) {//Be careful here is less than equals, do not write less than the $(*LOC2) + +; $     } -      while(UGLY_ARR[*LOC3] *3<= ugly_arr[*index]) { -(*LOC3) + +; the     } -      while(UGLY_ARR[*LOC5] *5<= ugly_arr[*index]) {Wuyi(*LOC5) + +; the     } -     if(UGLY_ARR[*LOC2] *2< ugly_arr[*loc3]*3) { Wu         return(UGLY_ARR[*LOC2] *2< UGLY_ARR[*LOC5] *5) ? UGLY_ARR[*LOC2] *2: UGLY_ARR[*LOC5] *5; -     } About     Else { $         return(UGLY_ARR[*LOC3] *3) < UGLY_ARR[*LOC5] *5? UGLY_ARR[*LOC3] *3: UGLY_ARR[*LOC5] *5; -     } - } -  A intGet_ugly_sec (intnum) { +     intugly_arr[ +]; the     intindex =0, value =1; -     intLoc2=0, loc3=0, loc5=0; $      while(value<=num) { theUgly_arr[index] =value; thecout << Ugly_arr[index] <<"\ t"; the         if(Index +1) %Ten==0) thecout <<Endl; -          inValue = next_ugly (Ugly_arr, &AMP;LOC2, &AMP;LOC3, &AMP;LOC5, &index); theindex++; the     } About     returnindex; the } the  the intMainintargcChar*argv[]) { +Get_ugly_fir ( the); -cout <<Endl; theGet_ugly_sec ( the);Bayi GetChar (); the     return 0; the}

(1) Description: A total of two methods are used, the first algorithm is low efficiency, simple programming, the second algorithm is high efficiency, programming is relatively complex.

(2) Method two ideas: The ugly number of the following must be the existence of the ugly number multiplied by 2, 3 or 5, to find a larger than the existing ugly number and is the smallest ugly number as the next ugly number (how to find is the key). With 2 respectively from the existing ugly number from the previous times the number of ugly, find the first is greater than the current value of all the ugly number and position, 3, 5 the same, and then multiply their results after the comparison, take the smallest. The next time you will be looking down from the previous location, there will be no redundancy.

Classic algorithm Detailed (9) Looking for ugly number

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.