Super Ugly Number (Number whose prime factors are in given set)


Super ugly numbers are positive numbers whose all prime factors are in the given prime list. Given a number n, the task is to find n’th Super Ugly number.

It may be assumed that given set of primes is sorted. Also, first Super Ugly number is 1 by convention.


Input  : primes[] = [2, 5]
         n = 5
Output : 8
Super Ugly numbers with given prime factors 
are 1, 2, 4, 5, 8, ...
Fifth Super Ugly number is 8

Input  : primes[] = [2, 3, 5]
         n = 50
Output : 243

Input : primes[] = [3, 5, 7, 11, 13]
        n = 9
Output: 21

In our previous post we discussed about Ugly Number. This problem is basically extension of Ugly Numbers.

A simple solution for this problem is to one by one pick each number starting from 1 and find its all primes factors, if all prime factors lie in the given set of primes that means number is Super Ugly. Repeat this process until we get n’th Super Ugly Number .

An efficient solution for this problem is similar to Method-2 of Ugly Number. Here is the algorithm :

  1. Let k be size of given array of prime numbers.
  2. Declare a set for super ugly numbers.
  3. Insert first ugly number (which is always 1) into set.
  4. Initialize array multiple_of[k] of size k with 0. Each element of this array is iterator for corresponding prime in primes[k] array.
  5. Initialize nextMultipe[k] array with primes[k]. This array behaves like next multiple variables of each prime in given primes[k] array i.e; nextMultiple[i] = primes[i] * ugly[++multiple_of[i]].
  6. Now loop until there are n elements in set ugly.
    a). Find minimum among current multiples of primes in nextMultiple[] array and insert it in the set of ugly numbers.
    b). Then find this current minimum is multiple of which prime .
    c). Increase iterator by 1 i.e; ++multiple_Of[i], for next multiple of current selected prime and update nextMultiple for it.

Below is implementation of above steps.

// C++ program to find n'th Super Ugly number
using namespace std;

// Function to get the nth super ugly number
// primes[]       --> given list of primes f size k
// ugly           --> set which holds all super ugly
//                    numbers from 1 to n
// k              --> Size of prime[]
int superUgly(int n, int primes[], int k)
    // nextMultiple holds multiples of given primes
    vector<int> nextMultiple(primes, primes+k);

    // To store iterators of all primes
    int multiple_Of[k];
    memset(multiple_Of, 0, sizeof(multiple_Of));

    // Create a set to store super ugly numbers and
    // store first Super ugly number
    set<int> ugly;

    // loop until there are total n Super ugly numbers
    // in set
    while (ugly.size() != n)
        // Find minimum element among all current
        // multiples of given prime
        int next_ugly_no = *min_element(nextMultiple.begin(),

        // insert this super ugly number in set

        // loop to find current minimum is multiple
        // of which prime
        for (int j=0; j<k; j++)
            if (next_ugly_no == nextMultiple[j])
                // increase iterator by one for next multiple
                // of current prime

                // this loop is similar to find  dp[++index[j]]
                // it -->  dp[++index[j]]
                set<int>::iterator it = ugly.begin();
                for (int i=1; i<=multiple_Of[j]; i++)

                nextMultiple[j] = primes[j] * (*it);

    // n'th super ugly number
    set<int>::iterator it = ugly.end();
    return *it;

/* Driver program to test above functions */
int main()
    int primes[] = {2,  5};
    int k = sizeof(primes)/sizeof(primes[0]);
    int n = 5;
    cout << superUgly(n, primes, k);
    return 0;



References :

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:

3.2 Average Difficulty : 3.2/5.0
Based on 23 vote(s)

Writing code in comment? Please use, generate link and share the link here.