Find the number of divisors of all numbers in the range [1, n]

Given an integer N. The task is to find the number of divisors of all the numbers in the range [1, N].

Examples:

Input: N = 5
Output: 1 2 2 3 2
divisors(1) = 1
divisors(2) = 1 and 2
divisors(3) = 1 and 3
divisors(4) = 1, 2 and 4
divisors(5) = 1 and 5

Input: N = 10
Output: 1 2 2 3 2 4 2 4 3 4

Approach: Create an array arr[] of the size (N + 1) where arr[i] stores the number of divisors of i. Now for every j from the range [1, N], increment all the elements which are divisible by j.
For example, if j = 3 then update arr[3], arr[6], arr[9], …



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the number of divisors
// of all numbers in the range [1, n]
void findDivisors(int n)
{
  
    // Array to store the count
    // of divisors
    int div[n + 1];
    memset(div, 0, sizeof div);
  
    // For every number from 1 to n
    for (int i = 1; i <= n; i++) {
  
        // Increase divisors count for
        // every number divisible by i
        for (int j = 1; j * i <= n; j++)
            div[i * j]++;
    }
  
    // Print the divisors
    for (int i = 1; i <= n; i++)
        cout << div[i] << " ";
}
  
// Driver code
int main()
{
    int n = 10;
    findDivisors(n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
  
class GFG 
      
    // Function to find the number of divisors 
    // of all numbers in the range [1, n] 
    static void findDivisors(int n) 
    
      
        // Array to store the count 
        // of divisors 
        int[] div = new int[n + 1]; 
      
        // For every number from 1 to n 
        for (int i = 1; i <= n; i++) 
        
      
            // Increase divisors count for 
            // every number divisible by i 
            for (int j = 1; j * i <= n; j++) 
                div[i * j]++; 
        
      
        // Print the divisors 
        for (int i = 1; i <= n; i++) 
            System.out.print(div[i]+" "); 
    
      
    // Driver code 
    public static void main(String args[]) 
    
        int n = 10
        findDivisors(n); 
    
  
// This code is contributed by Ryuga
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
# Function to find the number of divisors
# of all numbers in the range [1,n]
def findDivisors(n):
      
    # List to store the count
    # of divisors
    div = [0 for i in range(n + 1)]
      
    # For every number from 1 to n
    for i in range(1, n + 1):
          
        # Increase divisors count for
        # every number divisible by i
        for j in range(1, n + 1):
            if j * i <= n:
                div[i * j] += 1
  
    # Print the divisors
    for i in range(1, n + 1):
        print(div[i], end = " ")
  
# Driver Code
if __name__ == "__main__":
    n = 10
    findDivisors(n)
  
# This code is contributed by
# Vivek Kumar Singh
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to find the number of divisors
// of all numbers in the range [1, n]
static void findDivisors(int n)
{
  
    // Array to store the count
    // of divisors
    int[] div = new int[n + 1];
  
    // For every number from 1 to n
    for (int i = 1; i <= n; i++) 
    {
  
        // Increase divisors count for
        // every number divisible by i
        for (int j = 1; j * i <= n; j++)
            div[i * j]++;
    }
  
    // Print the divisors
    for (int i = 1; i <= n; i++)
        Console.Write(div[i]+" ");
}
  
// Driver code
static void Main()
{
    int n = 10;
    findDivisors(n);
}
}
  
// This code is contributed by mits
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to find the number of divisors 
// of all numbers in the range [1, n] 
function findDivisors($n
  
    // Array to store the count 
    // of divisors 
    $div = array_fill(0, $n + 2, 0); 
      
    // For every number from 1 to n 
    for ($i = 1; $i <= $n; $i++)
    
  
        // Increase divisors count for 
        // every number divisible by i 
        for ($j = 1; $j * $i <= $n; $j++) 
            $div[$i * $j]++; 
    
  
    // Print the divisors 
    for ($i = 1; $i <= $n; $i++) 
        echo $div[$i], " "
  
// Driver code 
$n = 10; 
findDivisors($n); 
  
// This code is contributed 
// by Arnab Kundu
?>
chevron_right

Output:
1 2 2 3 2 4 2 4 3 4





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.



Article Tags :
Practice Tags :