N | Java program to answer queries for N - th prime factor of a number ; 2 - D vector that stores prime factors ; Function to pre - store prime factors of all numbers till 10 ^ 6 ; calculate unique prime factors for every number till 10 ^ 6 ; find prime factors ; store if prime factor ; Function that returns answer for every query ; Driver Code ; Function to pre - store unique prime factors ; 1 st query ; 2 nd query ; 3 rd query ; 4 th query
import java . util . * ; class GFG { static int N = 1000001 ; static Vector < Integer > [ ] v = new Vector [ N ] ; static void preprocess ( ) { for ( int i = 1 ; i < N ; i ++ ) { int num = i ; for ( int j = 2 ; j <= Math . sqrt ( num ) ; j ++ ) { if ( num % j == 0 ) { v [ i ] . add ( j ) ; while ( num % j == 0 ) { num = num / j ; } } } if ( num > 2 ) v [ i ] . add ( num ) ; } } static int query ( int number , int n ) { return v [ number ] . get ( n - 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; preprocess ( ) ; int number = 6 , n = 1 ; System . out . print ( query ( number , n ) + "NEW_LINE"); number = 210 ; n = 3 ; System . out . print ( query ( number , n ) + "NEW_LINE"); number = 210 ; n = 2 ; System . out . print ( query ( number , n ) + "NEW_LINE"); number = 60 ; n = 2 ; System . out . print ( query ( number , n ) + "NEW_LINE"); } }