k-th prime factor of a given number

Given two numbers n and k, print k-th prime factor among all prime factors of n. For example, if the input number is 15 and k is 2, then output should be “5”. And if the k is 3, then output should be “-1” (there are less than k prime factors).

Examples:

Input : n = 225, k = 2        
Output : 3
Prime factors are 3 3 5 5. Second
prime factor is 3.

Input : n = 81, k = 5
Output : -1
Prime factors are 3 3 3 3

A Simple Solution is to first find prime factors of n. While finding prime factors, keep track of count. If count becomes k, we return current prime factor.

// Program to print kth prime factor
# include<bits/stdc++.h>
using namespace std;

// A function to generate prime factors of a
// given number n and return k-th prime factor
int kPrimeFactor(int n, int k)
{
    // Find the number of 2's that divide k
    while (n%2 == 0)
    {
        k--;
        n = n/2;
        if (k == 0)
         return 2;
    }

    // n must be odd at this point.  So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i+2)
    {
        // While i divides n, store i and divide n
        while (n%i == 0)
        {
            if (k == 1)
              return i;

            k--;
            n = n/i;
        }
    }

    // This condition is to handle the case where
    // n is a prime number greater than 2
    if (n > 2 && k == 1)
        return n;

    return -1;
}

// Driver Program
int main()
{
    int n = 12, k = 3;
    cout << kPrimeFactor(n, k) << endl;
    n = 14, k = 3;
    cout << kPrimeFactor(n, k) << endl;
    return 0;
}

Output:

3
-1

An Efficient Solution is to use Sieve of Eratosthenes. Note that this solution is efficient when we need k-th prime factor for multiple test cases. For a single case, previous approach is better.
The idea is to do preprocessing and store least prime factor of all numbers in given range. Once we have least prime factors stored in an array, we can find k-th prime factor by repeatedly dividing n with least prime factor while it is divisible, then repeating the process for reduced n.

// C++ program to find k-th prime factor using Sieve Of
// Eratosthenes. This program is efficient when we have
// a range of numbers and we
#include<bits/stdc++.h>
using namespace std;
const int MAX = 10001;

// Using SieveOfEratosthenes to find smallest prime
// factor of all the numbers.
// For example, if MAX is 10,
// s[2] = s[4] = s[6] = s[10] = 2
// s[3] = s[9] = 3
// s[5] = 5
// s[7] = 7
void sieveOfEratosthenes(int s[])
{
    // Create a boolean array "prime[0..MAX]" and
    // initialize all entries in it as false.
    vector <bool> prime(MAX+1, false);

    // Initializing smallest factor equal to 2
    // for all the even numbers
    for (int i=2; i<=MAX; i+=2)
        s[i] = 2;

    // For odd numbers less then equal to n
    for (int i=3; i<=MAX; i+=2)
    {
        if (prime[i] == false)
        {
            // s(i) for a prime is the number itself
            s[i] = i;

            // For all multiples of current prime number
            for (int j=i; j*i<=MAX; j+=2)
            {
                if (prime[i*j] == false)
                {
                    prime[i*j] = true;

                    // i is the smallest prime factor for
                    // number "i*j".
                    s[i*j] = i;
                }
            }
        }
    }
}

// Function to generate prime factors and return its
// k-th prime factor. s[i] stores least prime factor
// of i.
int kPrimeFactor(int n, int k, int s[])
{
    // Keep dividing n by least prime factor while
    // either n is not 1 or count of prime factors
    // is not k.
    while (n > 1)
    {
        if (k == 1)
          return s[n];

        // To keep tract of count of prime factors
        k--;

        // Divide n to find next prime factor
        n /= s[n];
    }

    return -1;
}

// Driver Program
int main()
{
    // s[i] is going to store prime factor
    // of i.
    int s[MAX+1];
    memset(s, -1, sizeof(s));
    sieveOfEratosthenes(s);

    int n = 12, k = 3;
    cout << kPrimeFactor(n, k, s) << endl;
    n = 14, k = 3;
    cout << kPrimeFactor(n, k, s) << endl;
    return 0;
}

Output:

3
-1

This article is contributed by Afzal Ansari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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:







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