Find the sum of the number of divisors - GeeksforGeeks

24 Nov, 2021
Given three integers A, B, C, the task is to find ΣAi=1 ΣBj=1ΣCk=1 d(i.j.k), where d(x) is the number of divisors of x. Answer can be very large, So, print answer modulo 109+7. Examples:  
Input: A = 2, B = 2, c = 2
Output: 20
Explanation: d(1.1.1) = d(1) = 1;
    d(1·1·2) = d(2) = 2;
    d(1·2·1) = d(2) = 2;
    d(1·2·2) = d(4) = 3;
    d(2·1·1) = d(2) = 2;
    d(2·1·2) = d(4) = 3;
    d(2·2·1) = d(4) = 3;
    d(2·2·2) = d(8) = 4. 

Input: A = 5, B = 6, C = 7
Output: 1520
 
Approach: 
Find the number of divisors of all numbers in the range [1, N]
Run three nested loops with iterators i, j, k starting from 1 to N
Then find d(i.j.k) using pre computed number of divisors.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>using namespace std; #define N 100005#define mod 1000000007 // To store the number of divisorsint cnt[N]; // Function to find the number of divisors// of all numbers in  the range 1 to nvoid Divisors(){    memset(cnt, 0, sizeof cnt);     // For every number 1 to n    for (int i = 1; i < N; i++) {         // Increase divisors count for every number        for (int j = 1; j * i < N; j++)            cnt[i * j]++;    }} // Function to find the sum of divisorsint Sumofdivisors(int A, int B, int C){    // To store sum    int sum = 0;     Divisors();     for (int i = 1; i <= A; i++) {        for (int j = 1; j <= B; j++) {            for (int k = 1; k <= C; k++) {                int x = i * j * k;                 // Count the divisors                sum += cnt[x];                if (sum >= mod)                    sum -= mod;            }        }    }     return sum;} // Driver codeint main(){     int A = 5, B = 6, C = 7;     // Function call    cout << Sumofdivisors(A, B, C);     return 0;}
// Java code for above given approachclass GFG{     static int N = 100005;    static int mod = 1000000007;     // To store the number of divisors    static int cnt[] = new int[N];     // Function to find the number of divisors    // of all numbers in the range 1 to n    static void Divisors()    {         // For every number 1 to n        for (int i = 1; i < N; i++)        {             // Increase divisors count for every number            for (int j = 1; j * i < N; j++)            {                cnt[i * j]++;            }        }    }     // Function to find the sum of divisors    static int Sumofdivisors(int A, int B, int C)    {        // To store sum        int sum = 0;         Divisors();         for (int i = 1; i <= A; i++)        {            for (int j = 1; j <= B; j++)            {                for (int k = 1; k <= C; k++)                {                    int x = i * j * k;                     // Count the divisors                    sum += cnt[x];                    if (sum >= mod)                    {                        sum -= mod;                    }                }            }        }         return sum;    }     // Driver code    public static void main(String[] args)    {        int A = 5, B = 6, C = 7;         // Function call        System.out.println(Sumofdivisors(A, B, C));    }} /* This code contributed by PrinciRaj1992 */
# Python3 code for above given approachN = 100005mod = 1000000007 # To store the number of divisorscnt = [0] * N; # Function to find the number of divisors# of all numbers in the range 1 to ndef Divisors() :     # For every number 1 to n    for i in range(1, N) :         # Increase divisors count        # for every number        for j in range(1, N // i) :            cnt[i * j] += 1; # Function to find the sum of divisorsdef Sumofdivisors(A, B, C) :         # To store sum    sum = 0;     Divisors();     for i in range(1,A + 1) :        for j in range(1, B + 1) :            for k in range(1, C + 1) :                x = i * j * k;                                 # Count the divisors                sum += cnt[x];                if (sum >= mod) :                    sum -= mod;     return sum; # Driver codeif __name__ == "__main__" :     A = 5; B = 6; C = 7;     # Function call    print(Sumofdivisors(A, B, C)); # This code is contributed by Ryuga
// C# code for above given approachusing System;     class GFG{     static int N = 100005;    static int mod = 1000000007;     // To store the number of divisors    static int []cnt = new int[N];     // Function to find the number of divisors    // of all numbers in the range 1 to n    static void Divisors()    {         // For every number 1 to n        for (int i = 1; i < N; i++)        {             // Increase divisors count for every number            for (int j = 1; j * i < N; j++)            {                cnt[i * j]++;            }        }    }     // Function to find the sum of divisors    static int Sumofdivisors(int A, int B, int C)    {        // To store sum        int sum = 0;         Divisors();         for (int i = 1; i <= A; i++)        {            for (int j = 1; j <= B; j++)            {                for (int k = 1; k <= C; k++)                {                    int x = i * j * k;                     // Count the divisors                    sum += cnt[x];                    if (sum >= mod)                    {                        sum -= mod;                    }                }            }        }         return sum;    }     // Driver code    public static void Main(String[] args)    {        int A = 5, B = 6, C = 7;         // Function call        Console.WriteLine(Sumofdivisors(A, B, C));    }} // This code contributed by Rajput-Ji
<script>    // Javascript code for above given approach         let N = 100005;    let mod = 1000000007;       // To store the number of divisors    let cnt = new Array(N);    cnt.fill(0);       // Function to find the number of divisors    // of all numbers in the range 1 to n    function Divisors()    {           // For every number 1 to n        for (let i = 1; i < N; i++)        {               // Increase divisors count for every number            for (let j = 1; j * i < N; j++)            {                cnt[i * j]++;            }        }    }       // Function to find the sum of divisors    function Sumofdivisors(A, B, C)    {        // To store sum        let sum = 0;           Divisors();           for (let i = 1; i <= A; i++)        {            for (let j = 1; j <= B; j++)            {                for (let k = 1; k <= C; k++)                {                    let x = i * j * k;                       // Count the divisors                    sum += cnt[x];                    if (sum >= mod)                    {                        sum -= mod;                    }                }            }        }           return sum;    }       let A = 5, B = 6, C = 7;       // Function call    document.write(Sumofdivisors(A, B, C));     // This code is contributed by divyeshrabdiya07.</script>
1520
 
ankthon
princiraj1992
Rajput-Ji
divyeshrabadiya07
sumitgumber28
divisors
school-programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Find first and last digits of a number
Count ways to reach the n'th stair
Program to convert a given number to words
Find Union and Intersection of two unsorted arrays