Find all divisors of first N natural numbers | Javascript implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call
var MAX = 100000 ; var divisor = Array . from ( Array ( MAX + 1 ) , ( ) => Array ( 0 ) ) ; function sieve ( ) { for ( var i = 1 ; i <= MAX ; ++ i ) { for ( var j = i ; j <= MAX ; j += i ) divisor [ j ] . push ( i ) ; } } function findNFactors ( n ) { for ( var i = 1 ; i <= n ; i ++ ) { document . write ( i + " " ) ; for ( var j = 0 ; j < divisor [ i ] . length ; j ++ ) { document . write ( divisor [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } var n = 5 ; sieve ( ) ; findNFactors ( n ) ;