Find the nth ugly number

Source: Internet
Author: User

Generally, the ugly number: all factors are only 2, 3, or 5. For convenience, 1 is also an ugly number. For example, 100 = 2*2*5*5, 12 = 2*2*3

Ugly data in the first 11: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 ,...

14 is not an ugly number, because 14 = 2*7, 7 is not in the range of 2, 3, 5

 

To determine whether a number is ugly:

Def isuugly (n): <br/> while (N % 2 = 0 ): <br/> N/= 2 <br/> while (N % 3 = 0 ): <br/> N/= 3 <br/> while (N % 5 = 0 ): <br/> N/= 5 <br/> If (n = 1): <br/> return true <br/> return false

 

1. Start from 1 and look up one by one. However, it is inefficient to judge whether a number is ugly:

Def getnthuglynumber (n): <br/> I = 1 <br/> CNT = 1 <br/> while (CNT <n ): <br/> I = I + 1 <br/> If (isuugly (I): <br/> CNT = CNT + 1 <br/> return I

An ugly number is the other ugly number multiplied by 2, multiplied by 3, or multiplied by 5 (except 1), such as 2 = 1*2, 3 = 1*3, 4 = 2*2, 5 = 1*5. We can generate an ugly number from small to big, put the N sequential ugly numbers in an array, and finally return the nth number. How to keep the array in order? For example, if we currently have the ugly number 1, which is the next ugly number? Currently, there is only 1, with 1*2 = 2, 1*3 = 3, 1*5 = 5, and the smallest of the three is greater than 1, so the second ugly number is 2, the current array is {1, 2}. What is the next ugly number? We can do this first:

Multiply the number of each number in the current array by 2, and take the number that is the smallest value greater than the maximum ugly number in the current array.

Multiply the number of each number in the current array by 3, and take the number that is the smallest value greater than the maximum ugly number in the current array.

Multiply the number of each number in the current array by 5, and take the number that is smaller than the maximum ugly number in the current array.

The last ugly number nextuugly = min (num2, num3, num5), such

1*2 = 2,2*2 = 4. Obtain num2 = 4.

1*3 = 3,2*3 = 5 get num3 = 3

1*5 = 5 5*5 = 10 get num5 = 5

Therefore, nextuugly = min (num2, num3, num5) = 3. In fact, there are many additional steps, which can be further improved:

Use Max to represent the current maximum ugly number. The initial max = 1, ptr2, ptr3, and ptr5 are all 0, pointing to the first element of the array, if nextuugly is obtained by uugly [ptr2] * 2, PTR 1 is added. To search forward, PTR + 1 points to the next big ugly number ~ Ptr3 and ptr5 are the same.

Def getnthuglynumber (n): <br/> uugly = [1] <br/> ptr2 = ptr3 = ptr5 = 0 <br/> for I in range (1, N ): <br/> num2 = uugly [ptr2] * 2 <br/> num3 = uugly [ptr3] * 3 <br/> num5 = uugly [ptr5] * 5 <br/> uugly. append (min (num2, num3, num5) <br/> If (uugly [I] = num2 ): <br/> ptr2 = ptr2 + 1 <br/> If (uugly [I] = num3 ): <br/> ptr3 = ptr3 + 1 <br/> If (uugly [I] = num5 ): <br/> ptr5 = ptr5 + 1 <br/> return uugly [-1]

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.