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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e5 ; vector < int > divisor [ MAX + 1 ] ; void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . push_back ( i ) ; } } void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << i << " - - > " ; for ( auto & divi : divisor [ i ] ) { cout << divi << " , ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 5 ; sieve ( ) ; findNFactors ( n ) ; }