VOOZH about

URL: https://www.geeksforgeeks.org/dsa/n-th-prime-factor-of-a-given-number/

⇱ N-th prime factor of a given number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

N-th prime factor of a given number

Last Updated : 11 Jul, 2025

Given Q queries which consist of two integers, one is number(1 <= number <= 106) and the other is N., the task is to find the N-th prime factor of the given number. 

Examples: 

Input: Number of Queries, Q = 4 
number = 6, N = 1 
number = 210, N = 3 
number = 210, N = 2 
number = 60, N = 2
Output: 



2

Explanations:

  • For number = 6, The prime factors 2 and 3. So, 2 is the 1st one.
  • For number = 210, The prime factors 2, 3, 5 and 7. So, 5 is the 3rd one.
  • For number = 210, The prime factors 2, 3, 5 and 7. So, 3 is the 2nd one.
  • For number = 60, The prime factors 2, 2, 3 and 5. So, 2 is the 2nd one.
     

A naive approach is to factorize every number and store the prime factors. Print the N-th prime factors thus stored. 
Time Complexity: O(log(n)) per query. 

An efficient approach is to pre-calculate all the prime factors of the number and store the numbers in a sorted order in a 2-D vector. Since the number will not be more than 106, the number of unique prime factors will be around 7-8 at max(because of 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 >= 106). Once the numbers are stored, the query can be answered in O(1) as the n-1th index will have the answer in numberth row. 

Below is the implementation of the above approach: 


Output
2
5
3
3

Time Complexity: O(1) per query and O(maxN * log(maxN)) for pre-processing, where maxN = 106.
Auxiliary Space: O(N * 8) in worst case

Another Efficient Approach:

Another efficient approach is to use Sieve of Eratosthenes. Note that this solution is efficient when we need k-th prime factor for multiple test cases.

Approach:

The approach 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.

Steps:

  1. The program starts by defining a function called "isPrime" that checks if a given number is prime or not. It returns a boolean value true if the number is prime and false otherwise.
  2. The program then defines a function called "AllPrimeFactors" that takes an integer argument N and returns a vector of all possible prime factors of the number. It uses a loop to iterate through all numbers from 2 to the square root of N and checks if each number is a factor of N. If it is, it adds the number to the vector and divides N by that number. It continues this process until N is no longer divisible by the current factor. If the remaining N is also a prime number, it adds it to the vector.
  3. The program defines a function called "query" that takes two integer arguments - a number and an integer k. It first calls the "AllPrimeFactors" function to get all possible prime factors of the number. It then checks if the number itself is a prime number and if the value of k is 1. If both conditions are true, it returns the number as the answer to the query. If the size of the vector of prime factors is less than k, it returns -1. Otherwise, it returns the k-th element of the vector as the answer to the query.
  4. The program then defines the "main" function which is the entry point of the program. It creates four queries by setting different values of "number" and "n". It calls the "query" function for each query and prints the result to the console.
  5. Finally, the program ends by returning 0 from the main function.

Output
2
5
3
2

Time Complexity: O(N*log(log(N))), where N is the number.
Auxiliary Space: O(N)

Comment