# Sum of multiplication of triplet of divisors of a number

• Last Updated : 14 May, 2021

Given an array arr[] of integers of size n. For each element, you have to print the summation of multiplication of every triplet formed using divisors of this element.
Examples:

Input: arr[] = {4}
Output:
4 has three divisors 1, 2 and 4.
1 * 2 * 4 = 8
Input: arr[] = {9, 5, 6}
Output: 27 0 72
9 has three divisors 1, 3 and 9. 1 * 3 * 9 = 27
5 has two divisors 1 and 5. So, no triplet is formed .
Similarly, 6 has four divisors 1, 2, 3 and 6. (1 * 2 * 3) + (1 * 3 * 6) + (2 * 3 * 6) + (1 * 6 * 2) = 72

Naive approach: Store all the divisors of a number and apply brute force method and for every triplet, add multiplication of these three elements to the answer.
Efficient approach: This method is similar to Sieve of Eratosthenes, which we use for finding all prime numbers of a range.

1. First, we make an array sum1 which stores the sum of all divisors of number x at sum1[x]. So first iterate through all the numbers less than maximum_Element and add this number to the sum of divisors of multiple of this number. Hence we will be able to store sum of all divisors of any number at that number’s position.
2. After filling the sum1 array, it’s time to fill the second array sum2 which will store the sum of multiplication of every pair divisor of number x at sum2[x].For filling this we go for every number similar to step1 and add multiplication of this number with to its higher values.
3. For filling sum3 array we will go similarly for all numbers less than max_Element and will add sum of multiplication of divisors of j such that i is a divisor of j.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;``#define ll long long``const` `ll max_Element = 1e6 + 5;` `// Global array declaration``int` `sum1[max_Element], sum2[max_Element], sum3[max_Element];` `// Function to find the sum of multiplication of``// every triplet in the divisors of a number``void` `precomputation(``int` `arr[], ``int` `n)``{``    ``// sum1[x] represents the sum of all the divisors of x``    ``for` `(``int` `i = 1; i < max_Element; i++)``        ``for` `(``int` `j = i; j < max_Element; j += i)` `            ``// Adding i to sum1[j] because i``            ``// is a divisor of j``            ``sum1[j] += i;` `    ``// sum2[x] represents the sum of all the divisors of x``    ``for` `(``int` `i = 1; i < max_Element; i++)``        ``for` `(``int` `j = i; j < max_Element; j += i)` `            ``// Here i is divisor of j and sum1[j] - i``            ``// represents sum of all divisors of``            ``// j which do not include i so we add``            ``// i * (sum1[j] - i) to sum2[j]``            ``sum2[j] += (sum1[j] - i) * i;` `    ``// In the above implementation we have considered``    ``// every pair two times so we have to divide``    ``// every sum2 array element by 2``    ``for` `(``int` `i = 1; i < max_Element; i++)``        ``sum2[i] /= 2;` `    ``// Here i is the divisor of j and we are trying to``    ``// add the sum of multiplication of all triplets of``    ``// divisors of j such that one of the divisors is i``    ``for` `(``int` `i = 1; i < max_Element; i++)``        ``for` `(``int` `j = i; j < max_Element; j += i)``            ``sum3[j] += i * (sum2[j] - i * (sum1[j] - i));` `    ``// In the above implementation we have considered``    ``// every triplet three times so we have to divide``    ``// every sum3 array element by 3``    ``for` `(``int` `i = 1; i < max_Element; i++)``        ``sum3[i] /= 3;` `    ``// Print the results``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << sum3[arr[i]] << ``" "``;``}` `// Driver code``int` `main()``{` `    ``int` `arr[] = { 9, 5, 6 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Precomputing``    ``precomputation(arr, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFGq``{` `static` `int` `max_Element = (``int``) (1e6 + ``5``);` `// Global array declaration``static` `int` `sum1[] = ``new` `int``[max_Element], sum2[] =``        ``new` `int``[max_Element], sum3[] = ``new` `int``[max_Element];` `// Function to find the sum of multiplication of``// every triplet in the divisors of a number``static` `void` `precomputation(``int` `arr[], ``int` `n)``{``    ``// sum1[x] represents the sum of all the divisors of x``    ``for` `(``int` `i = ``1``; i < max_Element; i++)``        ``for` `(``int` `j = i; j < max_Element; j += i)` `            ``// Adding i to sum1[j] because i``            ``// is a divisor of j``            ``sum1[j] += i;` `    ``// sum2[x] represents the sum of all the divisors of x``    ``for` `(``int` `i = ``1``; i < max_Element; i++)``        ``for` `(``int` `j = i; j < max_Element; j += i)` `            ``// Here i is divisor of j and sum1[j] - i``            ``// represents sum of all divisors of``            ``// j which do not include i so we add``            ``// i * (sum1[j] - i) to sum2[j]``            ``sum2[j] += (sum1[j] - i) * i;` `    ``// In the above implementation we have considered``    ``// every pair two times so we have to divide``    ``// every sum2 array element by 2``    ``for` `(``int` `i = ``1``; i < max_Element; i++)``        ``sum2[i] /= ``2``;` `    ``// Here i is the divisor of j and we are trying to``    ``// add the sum of multiplication of all triplets of``    ``// divisors of j such that one of the divisors is i``    ``for` `(``int` `i = ``1``; i < max_Element; i++)``        ``for` `(``int` `j = i; j < max_Element; j += i)``            ``sum3[j] += i * (sum2[j] - i * (sum1[j] - i));` `    ``// In the above implementation we have considered``    ``// every triplet three times so we have to divide``    ``// every sum3 array element by 3``    ``for` `(``int` `i = ``1``; i < max_Element; i++)``        ``sum3[i] /= ``3``;` `    ``// Print the results``    ``for` `(``int` `i = ``0``; i < n; i++)``        ``System.out.print(sum3[arr[i]] + ``" "``);``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``9``, ``5``, ``6` `};``    ``int` `n = arr.length;` `    ``// Precomputing``    ``precomputation(arr, n);``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python 3 implementation of the approach` `# Global array declaration``#global max_Element``max_Element ``=` `100005` `sum1 ``=` `[``0` `for` `i ``in` `range``(max_Element)]``sum2 ``=` `[``0` `for` `i ``in` `range``(max_Element)]``sum3 ``=` `[``0` `for` `i ``in` `range``(max_Element)]` `# Function to find the sum of multiplication of``# every triplet in the divisors of a number``def` `precomputation(arr, n):``    ` `    ``# global max_Element``    ``# sum1[x] represents the sum of``    ``# all the divisors of x``    ``for` `i ``in` `range``(``1``, max_Element, ``1``):``        ``for` `j ``in` `range``(i, max_Element, i):``            ` `            ``# Adding i to sum1[j] because i``            ``# is a divisor of j``            ``sum1[j] ``+``=` `i` `    ``# sum2[x] represents the sum of``    ``# all the divisors of x``    ``for` `i ``in` `range``(``1``, max_Element, ``1``):``        ``for` `j ``in` `range``(i, max_Element, i):``            ` `            ``# Here i is divisor of j and sum1[j] - i``            ``# represents sum of all divisors of``            ``# j which do not include i so we add``            ``# i * (sum1[j] - i) to sum2[j]``            ``sum2[j] ``+``=` `(sum1[j] ``-` `i) ``*` `i` `    ``# In the above implementation we have considered``    ``# every pair two times so we have to divide``    ``# every sum2 array element by 2``    ``for` `i ``in` `range``(``1``, max_Element, ``1``):``        ``sum2[i] ``=` `int``(sum2[i] ``/` `2``)` `    ``# Here i is the divisor of j and we are trying to``    ``# add the sum of multiplication of all triplets of``    ``# divisors of j such that one of the divisors is i``    ``for` `i ``in` `range``(``1``, max_Element, ``1``):``        ``for` `j ``in` `range``(i, max_Element, i):``            ``sum3[j] ``+``=` `i ``*` `(sum2[j] ``-` `i ``*``                           ``(sum1[j] ``-` `i))` `    ``# In the above implementation we have considered``    ``# every triplet three times so we have to divide``    ``# every sum3 array element by 3``    ``for` `i ``in` `range``(``1``, max_Element, ``1``):``        ``sum3[i] ``=` `int``(sum3[i] ``/` `3``)` `    ``# Print the results``    ``for` `i ``in` `range``(n):``        ``print``(sum3[arr[i]], end ``=` `" "``)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``9``, ``5``, ``6``]``    ``n ``=` `len``(arr)` `    ``# Precomputing``    ``precomputation(arr, n)` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `    ``static` `int` `max_Element = (``int``) (1e6 + 5);``    ` `    ``// Global array declaration``    ``static` `int` `[]sum1 = ``new` `int``[max_Element];``    ``static` `int` `[]sum2 = ``new` `int``[max_Element];``    ``static` `int` `[]sum3 = ``new` `int``[max_Element];``    ` `    ``// Function to find the sum of multiplication of``    ``// every triplet in the divisors of a number``    ``static` `void` `precomputation(``int` `[]arr, ``int` `n)``    ``{``        ``// sum1[x] represents the sum of all the divisors of x``        ``for` `(``int` `i = 1; i < max_Element; i++)``            ``for` `(``int` `j = i; j < max_Element; j += i)``    ` `                ``// Adding i to sum1[j] because i``                ``// is a divisor of j``                ``sum1[j] += i;``    ` `        ``// sum2[x] represents the sum of all the divisors of x``        ``for` `(``int` `i = 1; i < max_Element; i++)``            ``for` `(``int` `j = i; j < max_Element; j += i)``    ` `                ``// Here i is divisor of j and sum1[j] - i``                ``// represents sum of all divisors of``                ``// j which do not include i so we add``                ``// i * (sum1[j] - i) to sum2[j]``                ``sum2[j] += (sum1[j] - i) * i;``    ` `        ``// In the above implementation we have considered``        ``// every pair two times so we have to divide``        ``// every sum2 array element by 2``        ``for` `(``int` `i = 1; i < max_Element; i++)``            ``sum2[i] /= 2;``    ` `        ``// Here i is the divisor of j and we are trying to``        ``// add the sum of multiplication of all triplets of``        ``// divisors of j such that one of the divisors is i``        ``for` `(``int` `i = 1; i < max_Element; i++)``            ``for` `(``int` `j = i; j < max_Element; j += i)``                ``sum3[j] += i * (sum2[j] - i * (sum1[j] - i));``    ` `        ``// In the above implementation we have considered``        ``// every triplet three times so we have to divide``        ``// every sum3 array element by 3``        ``for` `(``int` `i = 1; i < max_Element; i++)``            ``sum3[i] /= 3;``    ` `        ``// Print the results``        ``for` `(``int` `i = 0; i < n; i++)``            ``Console.Write(sum3[arr[i]] + ``" "``);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]arr = { 9, 5, 6 };``        ``int` `n = arr.Length;``    ` `        ``// Precomputing``        ``precomputation(arr, n);``    ``}``}` `// This code has been contributed by Ryuga`

## PHP

 ``

## Javascript

 ``
Output:
`27 0 72`

My Personal Notes arrow_drop_up