Find all divisors of first N natural numbers | Java 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
import java . util . * ; class GFG { static int MAX = ( int ) 1e5 ; static Vector < Integer > [ ] divisor = new Vector [ MAX + 1 ] ; static void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . add ( i ) ; } } static void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( i + " - - > " ) ; for ( int divi : divisor [ i ] ) { System . out . print ( divi + " , ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . length ; i ++ ) divisor [ i ] = new Vector < Integer > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }