# Sum of divisors of factorial of a number

Given a number n, we need to calculate the sum of divisors of factorial of the number.

Examples:

```Input : 4
Output : 60
Factorial of 4 is 24. Divisors of 24 are
1 2 3 4 6 8 12 24, sum of these is 60.

Input : 6
Output : 2418
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A Simple Solution is to first compute factorial of given number, then count number divisors of the factorial. This solution is not efficient and may cause overflow due to factorial computation.

```// Java program to find sum of proper divisor of
// factorial of a number
import java.io.*;
import java.util.*;

public class Division
{
// function to calculate factorial
static int fac(int n)
{
if (n = = 0)
return 1;
return n*fac(n-1);
}

// function to calculate sum of divisor
static int div(int x)
{
int ans = 0;
for (int i = 1; i< = x; i++)
if (x%i == 0)
ans + = i;
return ans;
}

// Returns sum of divisors of n!
static int sumFactDiv(int n)
{
return div(fact(n));
}

// driver program
public static void main(String args[])
{
int n = 4;
System.out.println(sumFactDiv(n));
}
}
```

Output :

`60`

An efficient solution is based on Legendre’s formula. Below are the steps.

1. Find all prime numbers less than or equal to n (input number). We can use Sieve Algorithm for this. Let n be 6. All prime numbers less than 6 are {2, 3, 5}.
2. For each prime number p find the largest power of it that divides n!. We use below Legendre’s formula formula for this purpose.
3. The value of largest power that divides p is floor value of each term n/p + n/(p2) + n/(p3) + ……
Let these values be exp1, exp2, exp3, .. Using the above formula, we get below values for n = 6.

• The largest power of 2 that divides 6!, exp1 = 4.
• The largest power of 3 that divides 6!, exp2 = 2.
• The largest power of 5 that divides 6!, exp3 = 1.
4. The result is based on the Divisor Function.
```// C++ program to find sum of divisors in n!
#include<bits/stdc++.h>
#include<math.h>
using namespace std;

// allPrimes[] stores all prime numbers less
// than or equal to n.
vector<int> allPrimes;

// Fills above vector allPrimes[] for a given n
void sieve(int n)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// not a prime, else true.
vector<bool> prime(n+1, true);

// Loop to update prime[]
for (int p = 2; p*p <= n; p++)
{
// If prime[p] is not changed, then it
// is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p*2; i <= n; i += p)
prime[i] = false;
}
}

// Store primes in the vector allPrimes
for (int p = 2; p <= n; p++)
if (prime[p])
allPrimes.push_back(p);
}

// Function to find all result of factorial number
int factorialDivisors(int n)
{
sieve(n);  // create sieve

// Initialize result
int result = 1;

// find exponents of all primes which divides n
// and less than n
for (int i = 0; i < allPrimes.size(); i++)
{
// Current divisor
int p = allPrimes[i];

// Find the highest power (stored in exp)'
// of allPrimes[i] that divides n using
// Legendre's formula.
int exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes[i];
}

// Using the divisor function to calculate
// the sum
result = result*(pow(allPrimes[i], exp+1)-1)/
(allPrimes[i]-1);
}

return result;
}

// Driver program to run the cases
int main()
{
cout << factorialDivisors(4);
return 0;
}
```

Output:

```
15334088
```

This article is contributed by Pramod Kumar. 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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
4.6 Average Difficulty : 4.6/5.0
Based on 3 vote(s)