s [ i ] is going to store prime factor of i .
var s = Array . from ( { length : MAX + 1 } , ( _ , i ) => 0 ) ; sieveOfEratosthenes ( s ) ; var n = 12 , k = 3 ; document . write ( kPrimeFactor ( n , k , s ) + " " ) ; n = 14 ; k = 3 ; document . write ( kPrimeFactor ( n , k , s ) ) ;