k-th prime factor of a given number

Last Updated : 9 Jun, 2026

Given two numbers n and k, find the kth prime factor of n.
Prime factors are considered with repetition and are taken in non-decreasing order. If k is greater than the total number of prime factors of n, return -1.

Examples:

Input: n = 225, k = 2
Output: 3
Explanation: The prime factorization of 225 is 3 × 3 × 5 × 5. The prime factors in non-decreasing order are [3, 3, 5, 5]. The 2nd prime factor is 3.

Input: n = 81, k = 5
Output: -1
Explanation: The prime factorization of 81 is 3 × 3 × 3 × 3. The prime factors in non-decreasing order are [3, 3, 3, 3]. There is no 5th prime factor, so return -1.

Try It Yourself
redirect icon

Using Trial Division - O(√n) Time and O(1) Space

The idea is to divide n by every number starting from 2 up to √n. For each divisor that divides n, we keep dividing and decrementing k for every occurrence. Since we start from the smallest divisor and move upward, prime factors are naturally extracted in non-decreasing order. If k reaches 0 during this process we return that factor. After the loop, if n is still greater than 1 then n itself is a prime factor - we check if it is the kth one. If k is still not 0 after full factorization, we return -1.

C++
#include <iostream>
using namespace std;

int kthPrimeFactor(int n, int k) {

    // Traverse from 2 to sqrt(n) to find prime factors
    for (int div = 2; div * div <= n; div++) {

        // Divide out all occurrences of current factor
        while (n % div == 0) {

            // Decrement k for each prime factor found
            k--;

            // If k reaches 0 we found our answer
            if (k == 0)
                return div;

            n /= div;
        }
    }

    // If n is still greater than 1 it is a prime factor itself
    if (n > 1) {
        k--;

        // Check if this remaining prime is the kth factor
        if (k == 0)
            return n;
    }

    // k is greater than total number of prime factors
    return -1;
}

int main() {
    int n = 225, k = 2;
    cout << kthPrimeFactor(n, k) << endl;
    return 0;
}
Java
class GFG {

    static int kthPrimeFactor(int n, int k) {

        // Traverse from 2 to sqrt(n) to find prime factors
        for (int div = 2; div * div <= n; div++) {

            // Divide out all occurrences of current factor
            while (n % div == 0) {

                // Decrement k for each prime factor found
                k--;

                // If k reaches 0 we found our answer
                if (k == 0)
                    return div;

                n /= div;
            }
        }

        // If n is still greater than 1 it is a prime factor itself
        if (n > 1) {
            k--;

            // Check if this remaining prime is the kth factor
            if (k == 0)
                return n;
        }

        // k is greater than total number of prime factors
        return -1;
    }

    public static void main(String[] args) {
        int n = 225, k = 2;
        System.out.println(kthPrimeFactor(n, k));
    }
}
Python
def kthPrimeFactor(n, k):

    # Traverse from 2 to sqrt(n) to find prime factors
    div = 2
    while div * div <= n:

        # Divide out all occurrences of current factor
        while n % div == 0:

            # Decrement k for each prime factor found
            k -= 1

            # If k reaches 0 we found our answer
            if k == 0:
                return div

            n //= div

        div += 1

    # If n is still greater than 1 it is a prime factor itself
    if n > 1:
        k -= 1

        # Check if this remaining prime is the kth factor
        if k == 0:
            return n

    # k is greater than total number of prime factors
    return -1

if __name__ == "__main__":
    n = 225
    k = 2
    print(kthPrimeFactor(n, k))
C#
using System;

class GFG {

    static int kthPrimeFactor(int n, int k) {

        // Traverse from 2 to sqrt(n) to find prime factors
        for (int div = 2; div * div <= n; div++) {

            // Divide out all occurrences of current factor
            while (n % div == 0) {

                // Decrement k for each prime factor found
                k--;

                // If k reaches 0 we found our answer
                if (k == 0)
                    return div;

                n /= div;
            }
        }

        // If n is still greater than 1 it is a prime factor itself
        if (n > 1) {
            k--;

            // Check if this remaining prime is the kth factor
            if (k == 0)
                return n;
        }

        // k is greater than total number of prime factors
        return -1;
    }

    static void Main() {
        int n = 225, k = 2;
        Console.WriteLine(kthPrimeFactor(n, k));
    }
}
JavaScript
function kthPrimeFactor(n, k) {

    // Traverse from 2 to sqrt(n) to find prime factors
    for (let div = 2; div * div <= n; div++) {

        // Divide out all occurrences of current factor
        while (n % div === 0) {

            // Decrement k for each prime factor found
            k--;

            // If k reaches 0 we found our answer
            if (k === 0)
                return div;

            n /= div;
        }
    }

    // If n is still greater than 1 it is a prime factor itself
    if (n > 1) {
        k--;

        // Check if this remaining prime is the kth factor
        if (k === 0)
            return n;
    }

    // k is greater than total number of prime factors
    return -1;
}

// Driver code
let n = 225;
let k = 2;
console.log(kthPrimeFactor(n, k));

Output
3
Comment