Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included.

Given a number n, the task is to find n’th Ugly number.

Input  : n = 7
Output : 8

Input  : n = 10
Output : 12

Input  : n = 15
Output : 24

Input  : n = 150
Output : 5832

Method 1 (Simple)

Loop for all positive integers until ugly number count is smaller than n, if an integer is ugly than increment ugly number count.

To check if a number is ugly, divide the number by greatest divisible powers of 2, 3 and 5, if the number becomes 1 then it is an ugly number otherwise not.

For example, let us see how to check for 300 is ugly or not. Greatest divisible power of 2 is 4, after dividing 300 by 4 we get 75. Greatest divisible power of 3 is 3, after dividing 75 by 3 we get 25. Greatest divisible power of 5 is 25, after dividing 25 by 25 we get 1. Since we get 1 finally, 300 is ugly number.

[ad type=”banner”]

Implementation:

[pastacode lang=”c” manual=”%23%20include%3Cstdio.h%3E%0A%23%20include%3Cstdlib.h%3E%0A%20%0A%2F*This%20function%20divides%20a%20by%20greatest%20divisible%20%0A%20%20power%20of%20b*%2F%0Aint%20maxDivide(int%20a%2C%20int%20b)%0A%7B%0A%20%20while%20(a%25b%20%3D%3D%200)%0A%20%20%20a%20%3D%20a%2Fb%3B%20%0A%20%20return%20a%3B%0A%7D%20%20%20%0A%20%0A%2F*%20Function%20to%20check%20if%20a%20number%20is%20ugly%20or%20not%20*%2F%0Aint%20isUgly(int%20no)%0A%7B%0A%20%20no%20%3D%20maxDivide(no%2C%202)%3B%0A%20%20no%20%3D%20maxDivide(no%2C%203)%3B%0A%20%20no%20%3D%20maxDivide(no%2C%205)%3B%0A%20%20%20%0A%20%20return%20(no%20%3D%3D%201)%3F%201%20%3A%200%3B%0A%7D%20%20%20%20%0A%20%0A%2F*%20Function%20to%20get%20the%20nth%20ugly%20number*%2F%0Aint%20getNthUglyNo(int%20n)%0A%7B%0A%20%20int%20i%20%3D%201%3B%20%0A%20%20int%20count%20%3D%201%3B%20%20%20%2F*%20ugly%20number%20count%20*%2F%0A%20%0A%20%20%2F*Check%20for%20all%20integers%20untill%20ugly%20count%20%0A%20%20%20%20becomes%20n*%2F%0A%20%20while%20(n%20%3E%20count)%0A%20%20%7B%0A%20%20%20%20i%2B%2B%3B%20%20%20%20%20%20%0A%20%20%20%20if%20(isUgly(i))%0A%20%20%20%20%20%20count%2B%2B%3B%20%0A%20%20%7D%0A%20%20return%20i%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20unsigned%20no%20%3D%20getNthUglyNo(150)%3B%0A%20%20%20%20printf(%22150th%20ugly%20no.%20is%20%25d%20%22%2C%20%20no)%3B%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

150th ugly no. is 5832

This method is not time efficient as it checks for all integers until ugly number count becomes n, but space complexity of this method is O(1)

Method 2 (Use Dynamic Programming)

Here is a time efficient solution with O(n) extra space. The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …

[ad type=”banner”]

We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5. Then we use similar merge method as merge sort, to get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after.

1 Declare an array for ugly numbers:  ugly[n]
2 Initialize first ugly no:  ugly[0] = 1
3 Initialize three array index variables i2, i3, i5 to point to 
   1st element of the ugly array: 
        i2 = i3 = i5 =0; 
4 Initialize 3 choices for the next ugly no:
         next_mulitple_of_2 = ugly[i2]*2;
         next_mulitple_of_3 = ugly[i3]*3
         next_mulitple_of_5 = ugly[i5]*5;
5 Now go in a loop to fill all ugly numbers till 150:
For (i = 1; i < 150; i++ ) 
{
    /* These small steps are not optimized for good 
      readability. Will optimize them in C program */
    next_ugly_no  = Min(next_mulitple_of_2,
                        next_mulitple_of_3,
                        next_mulitple_of_5); 
    if (next_ugly_no  == next_mulitple_of_2) 
    {             
        i2 = i2 + 1;        
        next_mulitple_of_2 = ugly[i2]*2;
    } 
    if (next_ugly_no  == next_mulitple_of_3) 
    {             
        i3 = i3 + 1;        
        next_mulitple_of_3 = ugly[i3]*3;
     }            
     if (next_ugly_no  == next_mulitple_of_5)
     {    
        i5 = i5 + 1;        
        next_mulitple_of_5 = ugly[i5]*5;
     } 
     ugly[i] =  next_ugly_no       
}/* end of for loop */ 
6.return next_ugly_no
[ad type=”banner”]

Example:
Let us see how it works

initialize
   ugly[] =  | 1 |
   i2 =  i3 = i5 = 0;

First iteration
   ugly[1] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
            = Min(2, 3, 5)
            = 2
   ugly[] =  | 1 | 2 |
   i2 = 1,  i3 = i5 = 0  (i2 got incremented ) 

Second iteration
    ugly[2] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
             = Min(4, 3, 5)
             = 3
    ugly[] =  | 1 | 2 | 3 |
    i2 = 1,  i3 =  1, i5 = 0  (i3 got incremented ) 

Third iteration
    ugly[3] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
             = Min(4, 6, 5)
             = 4
    ugly[] =  | 1 | 2 | 3 |  4 |
    i2 = 2,  i3 =  1, i5 = 0  (i2 got incremented )

Fourth iteration
    ugly[4] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
              = Min(6, 6, 5)
              = 5
    ugly[] =  | 1 | 2 | 3 |  4 | 5 |
    i2 = 2,  i3 =  1, i5 = 1  (i5 got incremented )

Fifth iteration
    ugly[4] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
              = Min(6, 6, 10)
              = 6
    ugly[] =  | 1 | 2 | 3 |  4 | 5 | 6 |
    i2 = 3,  i3 =  2, i5 = 1  (i2 and i3 got incremented )

Will continue same way till I < 150
[pastacode lang=”c” manual=”%23%20include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F*%20Function%20to%20get%20the%20nth%20ugly%20number*%2F%0Aunsigned%20getNthUglyNo(unsigned%20n)%0A%7B%0A%20%20%20%20unsigned%20ugly%5Bn%5D%3B%20%2F%2F%20To%20store%20ugly%20numbers%0A%20%20%20%20unsigned%20i2%20%3D%200%2C%20i3%20%3D%200%2C%20i5%20%3D%200%3B%0A%20%20%20%20unsigned%20next_multiple_of_2%20%3D%202%3B%0A%20%20%20%20unsigned%20next_multiple_of_3%20%3D%203%3B%0A%20%20%20%20unsigned%20next_multiple_of_5%20%3D%205%3B%0A%20%20%20%20unsigned%20next_ugly_no%20%3D%201%3B%0A%20%0A%20%20%20%20ugly%5B0%5D%20%3D%201%3B%0A%20%20%20%20for%20(int%20i%3D1%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20next_ugly_no%20%3D%20min(next_multiple_of_2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min(next_multiple_of_3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20next_multiple_of_5))%3B%0A%20%20%20%20%20%20%20ugly%5Bi%5D%20%3D%20next_ugly_no%3B%0A%20%20%20%20%20%20%20if%20(next_ugly_no%20%3D%3D%20next_multiple_of_2)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20i2%20%3D%20i2%2B1%3B%0A%20%20%20%20%20%20%20%20%20%20%20next_multiple_of_2%20%3D%20ugly%5Bi2%5D*2%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20if%20(next_ugly_no%20%3D%3D%20next_multiple_of_3)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20i3%20%3D%20i3%2B1%3B%0A%20%20%20%20%20%20%20%20%20%20%20next_multiple_of_3%20%3D%20ugly%5Bi3%5D*3%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20if%20(next_ugly_no%20%3D%3D%20next_multiple_of_5)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20i5%20%3D%20i5%2B1%3B%0A%20%20%20%20%20%20%20%20%20%20%20next_multiple_of_5%20%3D%20ugly%5Bi5%5D*5%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20%2F*End%20of%20for%20loop%20(i%3D1%3B%20i%3Cn%3B%20i%2B%2B)%20*%2F%0A%20%20%20%20return%20next_ugly_no%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%20150%3B%0A%20%20%20%20cout%20%3C%3C%20getNthUglyNo(n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output :

5832

Algorithmic Paradigm: Dynamic Programming
Time Complexity: O(n)
Auxiliary Space: O(n)

[ad type=”banner”]