# Find all divisors of first N natural numbers

Given an integer N, the task is find all the divisors of numbers from 1 to N.

Note: 1 ≤ N ≤ 100000
Exmaples:

Input: N = 2
Output:
1 –>1
2 –>1, 2

Input: N = 5
Output:
1 –>1
2 –>1, 2
3 –>1, 3
4 –>1, 2, 4
5 –>1, 5

Naive Approach:

• Iterate over first N natural numbers using a loop variable (say i)
• Iterate over the natural numbers from 1 to i with a loop variable (say j) and check that i%j == 0. Then j is a divisor of the natural number i.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find all  ` `// the divisors of the first N ` `// natural numbers ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find the divisors ` `// of the first N natural numbers ` `void` `factors(``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``cout << ``"1 -->1\n"``; ` `     `  `    ``// Loop to find the divisors ` `    ``for` `(i = 2; i <= n; i++) { ` `        ``cout << i << ``" -->"``; ` `        ``for` `(j = 1; j <= i / 2; j++) { ` `            ``if` `(i % j == 0) ` `                ``cout << j << ``", "``; ` `        ``} ` `        ``cout << i << ``"\n"``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 5; ` `    ``factors(n); ` `} `

## Java

 `// Java implementation to find all  ` `// the divisors of the first N ` `// natural numbers ` `class` `GFG{ ` ` `  `// Function to find the divisors ` `// of the first N natural numbers ` `static` `void` `factors(``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``System.out.print(``"1 -->1\n"``); ` `     `  `    ``// Loop to find the divisors ` `    ``for``(i = ``2``; i <= n; i++) ` `    ``{ ` `       ``System.out.print(i + ``" -->"``); ` `       ``for``(j = ``1``; j <= i / ``2``; j++) ` `       ``{ ` `          ``if` `(i % j == ``0``) ` `              ``System.out.print(j + ``", "``); ` `       ``} ` `       ``System.out.print(i + ``"\n"``); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``5``; ` `    ``factors(n); ` `} ` `} ` ` `  `// This code is contributed by Rohit_ranjan `

## Python3

 `# Python3 implementation to find all  ` `# the divisors of the first N ` `# natural numbers ` ` `  `# Function to find the divisors ` `# of the first N natural numbers ` `def` `factors(n): ` ` `  `    ``i ``=` `0``; j ``=` `0``; ` `    ``print``(``"1 -->1"``); ` `     `  `    ``# Loop to find the divisors ` `    ``for` `i ``in` `range``(``2``, n ``+` `1``): ` `        ``print``(i, ``"-->"``, end ``=` `""); ` `        ``for` `j ``in` `range``(``1``, (i ``/``/` `2``) ``+` `1``): ` `            ``if` `(i ``%` `j ``=``=` `0``): ` `                ``print``(j, ``","``, end ``=` `""); ` `         `  `        ``print``(i, end ``=` `"\n"``); ` `     `  `# Driver Code ` `n ``=` `5``; ` `factors(n); ` ` `  `# This code is contributed by Code_Mech `

## C#

 `// C# implementation to find all  ` `// the divisors of the first N ` `// natural numbers ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find the divisors ` `// of the first N natural numbers ` `static` `void` `factors(``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``Console.Write(``"1 -->1\n"``); ` `     `  `    ``// Loop to find the divisors ` `    ``for``(i = 2; i <= n; i++) ` `    ``{ ` `        ``Console.Write(i + ``" -->"``); ` `        ``for``(j = 1; j <= i / 2; j++) ` `        ``{ ` `            ``if` `(i % j == 0) ` `                ``Console.Write(j + ``", "``); ` `        ``} ` `        ``Console.Write(i + ``"\n"``); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 5; ` `    ``factors(n); ` `} ` `} ` ` `  `// This code is contributed by Nidhi_biet `

Output:

```1 -->1
2 -->1, 2
3 -->1, 3
4 -->1, 2, 4
5 -->1, 5
```

Time Complexity: O(N2)

Better Approach:

• Iterate over the first N natural numbers using a loop variable.
• For number to find its divisors iterate from 2 to that number and check any one of them is a divisor of the given number.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find all ` `// the divisors of the first ` `// N natural numbers ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find the factors of ` `// the numbers from 1 to N ` `void` `factors(``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``cout << ``"1 -->1\n"``; ` `     `  `    ``// Loop to find the factors ` `    ``// of the first N natural  ` `    ``// numbers of the integer ` `    ``for` `(i = 2; i <= n; i++) { ` `        ``cout << i << ``" -->"``; ` `        ``for` `(j = 1; j * j <= i; j++) { ` `            ``if` `(i % j == 0){ ` `                ``cout << j << ``", "``; ` `                ``if` `(i / j != j) ` `                ``cout << i/j << ``", "``; ` `            ``} ` `        ``} ` `        ``cout << ``"\n"``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 5; ` `    ``factors(n); ` `} `

## Java

 `// Java implementation to find all ` `// the divisors of the first ` `// N natural numbers ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function to find the factors of ` `// the numbers from 1 to N ` `static` `void` `factors(``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``System.out.print(``"1 -->1\n"``); ` `     `  `    ``// Loop to find the factors ` `    ``// of the first N natural  ` `    ``// numbers of the integer ` `    ``for` `(i = ``2``; i <= n; i++)  ` `    ``{ ` `        ``System.out.print(i + ``" -->"``); ` `        ``for` `(j = ``1``; j * j <= i; j++)  ` `        ``{ ` `            ``if` `(i % j == ``0``) ` `            ``{ ` `                ``System.out.print(j + ``", "``); ` `                ``if` `(i / j != j) ` `                    ``System.out.print(i / j + ``", "``); ` `            ``} ` `        ``} ` `        ``System.out.print(``"\n"``); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `n = ``5``; ` `    ``factors(n); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

## C#

 `// C# implementation to find all ` `// the divisors of the first ` `// N natural numbers ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find the factors of ` `// the numbers from 1 to N ` `static` `void` `factors(``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``Console.Write(``"1 -->1\n"``); ` `     `  `    ``// Loop to find the factors ` `    ``// of the first N natural  ` `    ``// numbers of the integer ` `    ``for` `(i = 2; i <= n; i++)  ` `    ``{ ` `        ``Console.Write(i + ``" -->"``); ` `        ``for` `(j = 1; j * j <= i; j++)  ` `        ``{ ` `            ``if` `(i % j == 0) ` `            ``{ ` `                ``Console.Write(j + ``", "``); ` `                ``if` `(i / j != j) ` `                    ``Console.Write(i / j + ``", "``); ` `            ``} ` `        ``} ` `        ``Console.Write(``"\n"``); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 5; ` `    ``factors(n); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```1 -->1
2 -->1, 2,
3 -->1, 3,
4 -->1, 4, 2,
5 -->1, 5,
```

Time Complexty: O(N*sqrt(N))
Efficient Approach: The idea is to precompute the factors of the numbers with the help of the Seive of Eratosthenes. Then finally iterate over the first N natural numbers to find the factors.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the  ` `// factors of first N natural  ` `// numbers  ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `const` `int` `MAX = 1e5;  ` `   `  `// Initialize global divisor vector  ` `// array of sequence container  ` `vector<``int``> divisor[MAX + 1];  ` ` `  `// Calculate all  ` `// divisors of number  ` `void` `sieve()  ` `{  ` `    ``for` `(``int` `i = 1; i <= MAX; ++i) {  ` `        ``for` `(``int` `j = i; j <= MAX; j += i)  ` `            ``divisor[j].push_back(i);  ` `    ``}  ` `} ` ` `  `// Function to find the  ` `// factors of first n ` `// natural numbers ` `void` `findNFactors(``int` `n){ ` `    ``for``(``int` `i = 1; i <= n; i++){ ` `        ``cout << i << ``"-->"``; ` `        ``for` `(``auto` `&divi: divisor[i]){ ` `            ``cout << divi << ``", "``; ` `        ``} ` `        ``cout << ``"\n"``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 5; ` `    ``sieve(); ` `     `  `    ``// Function Call ` `    ``findNFactors(n); ` `} `

## Python3

 `# Python3 program to find the factors ` `# of first N natural numbers ` `MAX` `=` `100001` ` `  `# Initialize divisor list(array) ` `# of sequence container ` `divisor ``=` `[[] ``for` `x ``in` `range``(``MAX``)]  ` ` `  `# Calculate all divisors of a number ` `def` `sieve(): ` ` `  `    ``for` `i ``in` `range``(``1``, ``MAX``): ` `        ``for` `j ``in` `range``(i, ``MAX``, i): ` `            ``divisor[j].append(i) ` ` `  `# Function to find the factors of ` `# first n natural numbers ` `def` `findNFactors (n): ` ` `  `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `        ``print``(i, ``" --> "``, end ``=` `'') ` `         `  `        ``for` `divi ``in` `divisor[i]: ` `            ``print``(divi, ``", "``, end ``=` `'') ` `        ``print``() ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``n ``=` `5` `    ``sieve() ` ` `  `    ``# Function call ` `    ``findNFactors(n) ` ` `  `# This code is contributed by himanshu77 `

Output:

```1-->1,
2-->1, 2,
3-->1, 3,
4-->1, 2, 4,
5-->1, 5,
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.