# N-th prime factor of a given number

Given Q queries which consist of two integers, one is number(1 <= number <= 10^{6}) 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

5

3

36 has prime factors 2 and 3.

210 has prime factors 2, 3 and 6.

60 has prime factors 2 and 3.

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 10^{6}, the number of unique prime factors will be around 7-8 at max(*because of 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 >= 10 ^{6}*). Once the numbers are stored, the query can be answered in O(1) as the n-1

^{th}index will have the answer in number

^{th}row.

Below is the implementation of the above approach:

`// C++ program to answer queries ` `// for N-th prime factor of a number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `N = 1000001; ` ` ` `// 2-D vector that stores prime factors ` `vector<` `int` `> v[N]; ` ` ` `// Function to pre-store prime ` `// factors of all numbers till 10^6 ` `void` `preprocess() ` `{ ` ` ` `// calculate unique prime factors for ` ` ` `// every number till 10^6 ` ` ` `for` `(` `int` `i = 1; i < N; i++) { ` ` ` ` ` `int` `num = i; ` ` ` ` ` `// find prime factors ` ` ` `for` `(` `int` `j = 2; j <= ` `sqrt` `(num); j++) { ` ` ` `if` `(num % j == 0) { ` ` ` ` ` `// store if prime factor ` ` ` `v[i].push_back(j); ` ` ` ` ` `while` `(num % j == 0) { ` ` ` `num = num / j; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(num>2) ` ` ` `v[i].push_back(num); ` ` ` ` ` `} ` `} ` ` ` `// Function that returns answer ` `// for every query ` `int` `query(` `int` `number, ` `int` `n) ` `{ ` ` ` `return` `v[number][n - 1]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Function to pre-store unique prime factors ` ` ` `preprocess(); ` ` ` ` ` `// 1st query ` ` ` `int` `number = 6, n = 1; ` ` ` `cout << query(number, n) << endl; ` ` ` ` ` `// 2nd query ` ` ` `number = 210, n = 3; ` ` ` `cout << query(number, n) << endl; ` ` ` ` ` `// 3rd query ` ` ` `number = 210, n = 2; ` ` ` `cout << query(number, n) << endl; ` ` ` ` ` `// 4th query ` ` ` `number = 60, n = 2; ` ` ` `cout << query(number, n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 5 3 3

**Time Complexity:** O(1) per query and O(maxN * log(maxN)) for pre-processing, where maxN = 10^{6}.

**Auxiliary Space:** O(N * 8) in worst case

## Recommended Posts:

- k-th prime factor of a given number
- Find largest prime factor of a number
- Sum of largest prime factor of each number less than equal to n
- Find sum of a number and its maximum prime factor
- Prime Factor
- Exactly n distinct prime factor numbers from a to b
- Least prime factor of numbers till n
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Queries on the sum of prime factor counts in a range
- Nearest element with at-least one common prime factor
- Count of subarrays whose products don't have any repeating prime factor
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Find the total number of composite factor for a given number
- Factor Tree of a given Number
- Smallest number S such that N is a factor of S factorial or S!

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.