Calculate the 1500th number of ugly

Source: Internet
Author: User

Topic:
The numbers that contain only the mass factors 2, 3, and 5 are called ugly Numbers (Ugly number), for example: 2,3,4,5,6,8,9,10,12,15, etc., requiring an algorithm to get the 1500th ugly number.

One, sequential lookup, mainly used to verify that other methods are correct

Numbers are incremented to determine whether it is an ugly number

defif_ugly (num): while(num%2 = =0): Num= NUM/2 while(num%3 = =0): Num= NUM/3 while(num%5 = =0): Num= NUM/5if(num = = 1):        returnTrueElse:        returnFalse@timeitdefget_ugly (count): Res_list=[] Temp_count=0 Num= 2 while(temp_count<count):if(if_ugly (num)): Temp_count+ = 1res_list.append (num) num+ = 1returnRes_list

Second, taking into account that the ugly number is 2,3,5 multiple power multiplication, assuming that the 1500th ugly number is 2 n power, 3 m power, 5 I power of the product, first assumed a highest power Q (n+m+i<=q), N, M, I arrange combinations to generate a number of ugly numbers, and then arrange. But unfortunately this does not guarantee that the sorted queue is exactly the same as the actual number of ugly sequence, that is, some numbers in the queue is lost, but at least guaranteed that the 2 Q power and the previous sequence is correct, if the 2 Q power and the previous sequence is not enough 1500, you need to change the power to q+1

defget_times (num): Temp_count= 1Temp= 2 while(temp<num): Temp_count+ = 1Temp*= 2returnTemp_count@time_used_itdefGet_ugly2 (count): Res_list=[] times=get_times (count) while(True): Res_list= []         forNum2inchRange (times+1):             forNum3inchRange (times+1):                 forNum5inchRange (times+1): Res_list.append (Times_num (2, num2) * Times_num (3, num3) *times_num (5, NUM5)) Res_list.sort ()if(Res_list.index (Times_num (2, Times)) +1 >count): Break Times+ = 1returnRes_list[:count]

Third, assuming that there are already ugly queues [1,2,3,5 ...], the next ugly number is definitely the smallest number in the queue that is not in the existing list of ugly numbers, multiplied by the 2,3,5. But we do not need to multiply it all by comparison, because the smallest should be the previous number.

The 2,3,5 is multiplied by the number of positions in the queue, respectively Ind2, Ind3, Ind5, respectively, in the position of the multiplier. Initialize the list of ugly teams as [1],ind2, Ind3, Ind5 initialized to 0,2,3,5 respectively with the corresponding IND indicated in the position of the ugly number multiplied, get the three new number to take the smallest one, if this number in the original ugly queue does not exist, then the next ugly number, added to the end of the queue, The IND (one of Ind2, Ind3, Ind5) with the ugly number is added to the next position, and the next cycle is performed.

defGet_ugly3 (count): Res_list= [1] Ind2=0 Ind3=0 Ind5=0 Temp_count=0 Mult_nums= [Res_list[ind2]*2, res_list[ind3]*3, res_list[ind5]*5]         while(temp_count<count): New_min=min (mult_nums)if(new_min>res_list[-1]): Res_list.append (new_min) Temp_count+ = 1if(New_min = = Res_list[ind2]*2): Ind2+ = 1Mult_nums[0]= Res_list[ind2]*2elif(New_min = = Res_list[ind3]*3): Ind3+ = 1mult_nums[1] = Res_list[ind3]*3Else: Ind5+ = 1mult_nums[2] = res_list[ind5]*5returnRes_list

Spend time separately

619.2290817411.062788427490.00205379498129

The three parts of the method of "note" refer to http://blog.csdn.net/joanlynnlove/article/details/7623799

Calculate the 1500th number of ugly

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.