VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-divisors-factorial-number/

⇱ Sum of divisors of factorial of a number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of divisors of factorial of a number

Last Updated : 23 Jul, 2025

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

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

Below is the implementation of the above approach:  

Output : 

60

Time Complexity: O(n!)

Auxiliary Space: O(1)


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 Legendre’s formula for this purpose.
    • 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.
  3. The result is based on the Divisor Function

Output: 

60

Time Complexity: O(n*log(log(n)))

Auxiliary Space: O(n)


 

Comment