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 factorial of given number, then count number divisors of the factorial. This solution is not efficient and may cause overflow due to factorial computation.
Below is the implementation of above approach:
C++
// C++ program to find sum of proper divisor of // factorial of a number #include <bits/stdc++.h> using namespace std; // function to calculate factorial int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); } // function to calculate sum of divisor 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! int sumFactDiv(int n) { return div(fact(n)); } // Driver Code int main() { int n = 4; cout << sumFactDiv(n); } // This code is contributed // by Akanksha Rai |
C
// C program to find sum of proper divisor of // factorial of a number #include <stdio.h> // function to calculate factorial int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); } // function to calculate sum of divisor 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! int sumFactDiv(int n) { return div(fact(n)); } // driver program int main() { int n = 4; printf("%d",sumFactDiv(n)); } |
Java
// 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 fact(int n) { if (n == 0) return 1; return n*fact(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)); } } |
Python3
# Python 3 program to find sum of proper # divisor of factorial of a number # function to calculate factorial def fact(n): if (n == 0): return 1 return n * fact(n - 1) # function to calculate sum # of divisor def div(x): ans = 0; for i in range(1, x + 1): if (x % i == 0): ans += i return ans # Returns sum of divisors of n! def sumFactDiv(n): return div(fact(n)) # Driver Code n = 4print(sumFactDiv(n)) # This code is contributed # by Rajput-Ji |
C#
// C# program to find sum of proper // divisor of factorial of a number using System; 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(fac(n)); } // Driver Code public static void Main() { int n = 4; Console.Write(sumFactDiv(n)); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP program to find sum of proper // divisor of factorial of a number // function to calculate factorial function fact($n) { if ($n == 0) return 1; return $n * fact($n - 1); } // function to calculate sum of divisor function div($x) { $ans = 0; for ($i = 1; $i<= $x; $i++) if ($x % $i == 0) $ans += $i; return $ans; } // Returns sum of divisors of n! function sumFactDiv($n) { return div(fact($n)); } // Driver Code $n = 4; echo sumFactDiv($n); // This code is contributed // by Akanksha Rai ?> |
Output :
60
An efficient solution is based on Legendre’s formula. Below are the steps.
- 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}.
- For each prime number p find the largest power of it that divides n!. We use below Legendre’s formula 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.
- The result is based on the Divisor Function.
C++
// C++ program to find sum of divisors in n!#include<bits/stdc++.h>#include<math.h>usingnamespacestd;// allPrimes[] stores all prime numbers less// than or equal to n.vector<int> allPrimes;// Fills above vector allPrimes[] for a given nvoidsieve(intn){// 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(intp = 2; p*p <= n; p++){// If prime[p] is not changed, then it// is a primeif(prime[p] ==true){// Update all multiples of pfor(inti = p*2; i <= n; i += p)prime[i] =false;}}// Store primes in the vector allPrimesfor(intp = 2; p <= n; p++)if(prime[p])allPrimes.push_back(p);}// Function to find all result of factorial numberintfactorialDivisors(intn){sieve(n);// create sieve// Initialize resultintresult = 1;// find exponents of all primes which divides n// and less than nfor(inti = 0; i < allPrimes.size(); i++){// Current divisorintp = allPrimes[i];// Find the highest power (stored in exp)'// of allPrimes[i] that divides n using// Legendre's formula.intexp= 0;while(p <= n){exp=exp+ (n/p);p = p*allPrimes[i];}// Using the divisor function to calculate// the sumresult = result*(pow(allPrimes[i],exp+1)-1)/(allPrimes[i]-1);}// return total divisorsreturnresult;}// Driver program to run the casesintmain(){cout << factorialDivisors(4);return0;}Java
// Java program to find sum of divisors in n!importjava.util.*;classGFG{// allPrimes[] stores all prime numbers less// than or equal to n.staticArrayList<Integer> allPrimes=newArrayList<Integer>();// Fills above vector allPrimes[] for a given nstaticvoidsieve(intn){// 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.boolean[] prime=newboolean[n+1];// Loop to update prime[]for(intp =2; p*p <= n; p++){// If prime[p] is not changed, then it// is a primeif(prime[p] ==false){// Update all multiples of pfor(inti = p*2; i <= n; i += p)prime[i] =true;}}// Store primes in the vector allPrimesfor(intp =2; p <= n; p++)if(prime[p]==false)allPrimes.add(p);}// Function to find all result of factorial numberstaticintfactorialDivisors(intn){sieve(n);// create sieve// Initialize resultintresult =1;// find exponents of all primes which divides n// and less than nfor(inti =0; i < allPrimes.size(); i++){// Current divisorintp = allPrimes.get(i);// Find the highest power (stored in exp)'// of allPrimes[i] that divides n using// Legendre's formula.intexp =0;while(p <= n){exp = exp + (n/p);p = p*allPrimes.get(i);}// Using the divisor function to calculate// the sumresult = result*((int)Math.pow(allPrimes.get(i), exp+1)-1)/(allPrimes.get(i)-1);}// return total divisorsreturnresult;}// Driver program to run the casespublicstaticvoidmain(String[] args){System.out.println(factorialDivisors(4));}}// This code is contributed by mitsPython3
# Python3 program to find sum of divisors in n!# allPrimes[] stores all prime numbers# less than or equal to n.allPrimes=[];# Fills above vector allPrimes[]# for a given ndefsieve(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.prime=[True]*(n+1);# Loop to update prime[]p=2;while(p*p <=n):# If prime[p] is not changed,# then it is a primeif(prime[p]==True):# Update all multiples of pforiinrange(p*2, n+1, p):prime[i]=False;p+=1;# Store primes in the vector allPrimesforpinrange(2, n+1):if(prime[p]):allPrimes.append(p);# Function to find all result of factorial numberdeffactorialDivisors(n):sieve(n);# create sieve# Initialize resultresult=1;# find exponents of all primes which# divides n and less than nforiinrange(len(allPrimes)):# Current divisorp=allPrimes[i];# Find the highest power (stored in exp)'# of allPrimes[i] that divides n using# Legendre's formula.exp=0;while(p <=n):exp=exp+int(n/p);p=p*allPrimes[i];# Using the divisor function to# calculate the sumresult=int(result*(pow(allPrimes[i], exp+1)-1)/(allPrimes[i]-1));# return total divisorsreturnresult;# Driver Codeprint(factorialDivisors(4));# This code is contributed by mitsC#
// C# program to find sum of divisors in n!usingSystem;usingSystem.Collections;classGFG{// allPrimes[] stores all prime numbers less// than or equal to n.staticArrayList allPrimes=newArrayList();// Fills above vector allPrimes[] for a given nstaticvoidsieve(intn){// 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.bool[] prime=newbool[n+1];// Loop to update prime[]for(intp = 2; p*p <= n; p++){// If prime[p] is not changed, then it// is a primeif(prime[p] ==false){// Update all multiples of pfor(inti = p*2; i <= n; i += p)prime[i] =true;}}// Store primes in the vector allPrimesfor(intp = 2; p <= n; p++)if(prime[p]==false)allPrimes.Add(p);}// Function to find all result of factorial numberstaticintfactorialDivisors(intn){sieve(n);// create sieve// Initialize resultintresult = 1;// find exponents of all primes which divides n// and less than nfor(inti = 0; i < allPrimes.Count; i++){// Current divisorintp = (int)allPrimes[i];// Find the highest power (stored in exp)'// of allPrimes[i] that divides n using// Legendre's formula.intexp = 0;while(p <= n){exp = exp + (n/p);p = p*(int)allPrimes[i];}// Using the divisor function to calculate// the sumresult = result*((int)Math.Pow((int)allPrimes[i], exp+1)-1)/((int)allPrimes[i]-1);}// return total divisorsreturnresult;}// Driver program to run the casesstaticvoidMain(){Console.WriteLine(factorialDivisors(4));}}// This code is contributed by mitsPHP
<?php// PHP program to find sum of divisors in n!// allPrimes[] stores all prime numbers less// than or equal to n.$allPrimes=array();// Fills above vector allPrimes[] for a given nfunctionsieve($n){global$allPrimes;// 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.$prime=array_fill(0,$n+1,true);// Loop to update prime[]for($p= 2;$p*$p<=$n;$p++){// If prime[p] is not changed, then it// is a primeif($prime[$p] == true){// Update all multiples of pfor($i=$p*2;$i<=$n;$i+=$p)$prime[$i] = false;}}// Store primes in the vector allPrimesfor($p= 2;$p<=$n;$p++)if($prime[$p])array_push($allPrimes,$p);}// Function to find all result of factorial numberfunctionfactorialDivisors($n){global$allPrimes;sieve($n);// create sieve// Initialize result$result= 1;// find exponents of all primes which divides n// and less than nfor($i= 0;$i<count($allPrimes);$i++){// Current divisor$p=$allPrimes[$i];// Find the highest power (stored in exp)'// of allPrimes[i] that divides n using// Legendre's formula.$exp= 0;while($p<=$n){$exp=$exp+ (int)($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 total divisorsreturn$result;}// Driver program to run the casesprint(factorialDivisors(4));// This code is contributed by mits?>Output:
60
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
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.

