[Post] Looking for ugly data

Source: Internet
Author: User

From: http://zhedahht.blog.163.com/blog/static/2541117420094245366965/

Question: The numbers that only contain factors 2, 3, and 5 are called the ugly number ). For example, values 6 and 8 are ugly, but 14 is not because it contains factor 7. In habits, we regard 1 as the first ugly number. Calculate the number of ugliness in the ascending order.

Analysis: This is a widely used interview question on the Internet. It is said that Google has used this question.

A number M is a factor of another number N, which means that N can be divisible by M, that is, N % m = 0. According to the definition of the ugly number, the ugly number can only be divided by 2, 3, and 5. That is to say, if a number can be divisible by 2, we divide it by 2 consecutively; if it can be divisible by 3, it is divided by 3 consecutively; if it can be divisible by 5, it is divided by 5 in a row. If we get 1 at the end, this number is the ugly number, otherwise it is not.

Based on the previous analysis, we can write the following functions to determine whether a number is ugly:

bool IsUgly(int number)
{
while(number % 2 == 0)
number /= 2;
while(number % 3 == 0)
number /= 3;
while(number % 5 == 0)
number /= 5;
return (number == 1) ? true : false;
}

Next, we only need to judge in order whether each integer is an ugly number, that is:

 

int GetUglyNumber_Solution1(int index)
{
if(index <= 0)
return 0;

int number = 0;
int uglyFound = 0;
while(uglyFound < index) {
++number;
if(IsUgly(number))
++uglyFound;
}
return number;
}

You only need to input the 1500 parameter in the getuglynumber_solution1 function to get 1,500th ugly numbers. The algorithm is intuitive and the code is concise, but the biggest problem is that each integer needs to be calculated. Even if a number is not an ugly number, we still need to perform the remainder and Division operations on it. Therefore, the time efficiency of this algorithm is not very high.

Next we will analyze this problem with another way of thinking, and try to calculate only the ugly number, rather than the time spent on the integer rather than the ugly number. According to the definition of the ugly number, the ugly number should be the result of multiplying the other ugly number by 2, 3 or 5 (except 1 ). Therefore, we can create an array where numbers are sorted by the ugly numbers, and each ugly number in it is obtained by multiplying the ugly number in front by 2, 3, or 5.

The key to this idea is how to ensure that the ugly numbers in the array are sorted in order. We suppose there are already several ugly numbers in the array, and there are arrays after sorting. Let's record the largest ugly number as M. Now let's generate the next ugly number, which must be the result of multiplying the previous ugly number by 2, 3, or 5. First, we should multiply each ugly number by 2. When multiplied by 2, several results are smaller than or equal to M. Since we generate them in order, the values smaller than or equal to m must already be in the array, so we don't need to consider them again; we will get several results larger than m, but we only need the first result larger than m, because we want the ugly number to be generated in ascending order, and we will discuss other larger results later. The result of multiplying the first one by 2 with a value greater than m is marked as M2. Similarly, we multiply every ugly number by 3 and 5 to get the first result m3 and M5 larger than M. The next ugly number should be the least among the three numbers M2, M3, and M5.

In the previous analysis, we mentioned that multiplying each existing ugly number by 2, 3, and 5 is actually not required, because the existing ugly number exists in the array in order. For multiplied by 2, there must be an ugly number T2, and the result of multiplying each ugly number before it by 2 will be smaller than the largest ugly number, the result of multiplying every ugly number after it by 2 is too large. We only need to write down the location of the ugly number, and update the T2. For multiplied by 3 and 5, there is the same T3 and T5.

With these analyses, we can easily write the following code:

int GetUglyNumber_Solution2(int index)
{
if(index <= 0) return 0;

int *pUglyNumbers = new int[index];
pUglyNumbers[0] = 1;
int nextUglyIndex = 1;

int *pMultiply2 = pUglyNumbers;
int *pMultiply3 = pUglyNumbers;
int *pMultiply5 = pUglyNumbers;

while(nextUglyIndex < index)
{
int min = Min(*pMultiply2 * 2, *pMultiply3 * 3, *pMultiply5 * 5);
pUglyNumbers[nextUglyIndex] = min;
while(*pMultiply2 * 2 <= pUglyNumbers[nextUglyIndex])
++pMultiply2;
while(*pMultiply3 * 3 <= pUglyNumbers[nextUglyIndex])
++pMultiply3;
while(*pMultiply5 * 5 <= pUglyNumbers[nextUglyIndex])
++pMultiply5;
++nextUglyIndex;
}

int ugly = pUglyNumbers[nextUglyIndex - 1];
delete[] pUglyNumbers;
return ugly;
}

int Min(int number1, int number2, int number3)
{
int min = (number1 < number2) ? number1 : number2;
min = (min < number3) ? min : number3;
return min;
}

Compared with the first approach, this algorithm does not need to perform any calculations on non-ugly integers, so the time complexity is much lower. Interested readers can calculate the running time of the getuglynumber_solution1 (1500) and getuglynumber_solution2 (1500) functions respectively. Of course, we also need to point out that the second algorithm needs an array and requires additional memory to store the generated ugly number. The first algorithm does not have such memory overhead.

 

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.