Find all divisors of first N natural numbers | C # 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
using System ; using System . Collections . Generic ; public class GFG { static int MAX = ( int ) 1e5 ; static List < int > [ ] divisor = new List < int > [ 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 ++ ) { Console . Write ( i + " - - > " ) ; foreach ( int divi in divisor [ i ] ) { Console . Write ( divi + " , ▁ " ) ; } Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . Length ; i ++ ) divisor [ i ] = new List < int > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }