N | Javascript 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 ; Function to pre - store unique prime factors ; 1 st query ; 2 nd query ; 3 rd query ; 4 th query
const N = 1000001 ; let v = new Array ( ) ; for ( let i = 0 ; i < N ; i ++ ) { v . push ( new Array ( ) ) } function preprocess ( ) { for ( let i = 1 ; i < N ; i ++ ) { let num = i ; for ( let j = 2 ; j <= Math . sqrt ( num ) ; j ++ ) { if ( num % j == 0 ) { v [ i ] . push ( j ) ; while ( num % j == 0 ) { num = num / j ; } } } if ( num > 2 ) v [ i ] . push ( num ) ; } } function query ( number , n ) { return v [ number ] [ n - 1 ] ; } preprocess ( ) ; let number = 6 , n = 1 ; document . write ( query ( number , n ) + " " ) ; number = 210 , n = 3 ; document . write ( query ( number , n ) + " " ) ; number = 210 , n = 2 ; document . write ( query ( number , n ) + " " ) ; number = 60 , n = 2 ; document . write ( query ( number , n ) + " " ) ;