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
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:
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
if (n == 0)
return 1;
return n * fact(n - 1);
}
int div ( int x)
{
int ans = 0;
for ( int i = 1; i<= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
int sumFactDiv( int n)
{
return div (fact(n));
}
int main()
{
int n = 4;
cout << sumFactDiv(n);
}
|
C
#include <stdio.h>
int fact( int n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
int div ( int x) {
int ans = 0;
for ( int i = 1; i<= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
int sumFactDiv( int n) {
return div (fact(n));
}
int main() {
int n = 4;
printf ( "%d" ,sumFactDiv(n));
}
|
Java
import java.io.*;
import java.util.*;
public class Division
{
static int fact( int n)
{
if (n == 0 )
return 1 ;
return n*fact(n- 1 );
}
static int div( int x)
{
int ans = 0 ;
for ( int i = 1 ; i<= x; i++)
if (x%i == 0 )
ans += i;
return ans;
}
static int sumFactDiv( int n)
{
return div(fact(n));
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(sumFactDiv(n));
}
}
|
Python3
def fact(n):
if (n = = 0 ):
return 1
return n * fact(n - 1 )
def div(x):
ans = 0 ;
for i in range ( 1 , x + 1 ):
if (x % i = = 0 ):
ans + = i
return ans
def sumFactDiv(n):
return div(fact(n))
n = 4
print (sumFactDiv(n))
|
C#
using System;
class Division {
static int fac( int n)
{
if (n == 0)
return 1;
return n * fac(n - 1);
}
static int div( int x)
{
int ans = 0;
for ( int i = 1; i <= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
static int sumFactDiv( int n)
{
return div(fac(n));
}
public static void Main()
{
int n = 4;
Console.Write(sumFactDiv(n));
}
}
|
PHP
<?php
function fact( $n )
{
if ( $n == 0)
return 1;
return $n * fact( $n - 1);
}
function div( $x )
{
$ans = 0;
for ( $i = 1; $i <= $x ; $i ++)
if ( $x % $i == 0)
$ans += $i ;
return $ans ;
}
function sumFactDiv( $n )
{
return div(fact( $n ));
}
$n = 4;
echo sumFactDiv( $n );
?>
|
Javascript
<script>
function fact(n)
{
if (n == 0)
return 1;
return n * fact(n - 1);
}
function div(x)
{
let ans = 0;
for (let i = 1; i<= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
function sumFactDiv(n)
{
return div(fact(n));
}
let n = 4;
document.write(sumFactDiv(n));
</script>
|
Output :
60
Time Complexity: O(n!)
Auxiliary Space: O(1)
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 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.
- The result is based on the Divisor Function.
C++
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
vector< int > allPrimes;
void sieve( int n)
{
vector< bool > prime(n+1, true );
for ( int p = 2; p*p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p*2; i <= n; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p <= n; p++)
if (prime[p])
allPrimes.push_back(p);
}
int factorialDivisors( int n)
{
sieve(n);
int result = 1;
for ( int i = 0; i < allPrimes.size(); i++)
{
int p = allPrimes[i];
int exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes[i];
}
result = result*( pow (allPrimes[i], exp +1)-1)/
(allPrimes[i]-1);
}
return result;
}
int main()
{
cout << factorialDivisors(4);
return 0;
}
|
Java
import java.util.*;
class GFG{
static ArrayList<Integer> allPrimes= new ArrayList<Integer>();
static void sieve( int n)
{
boolean [] prime= new boolean [n+ 1 ];
for ( int p = 2 ; p*p <= n; p++)
{
if (prime[p] == false )
{
for ( int i = p* 2 ; i <= n; i += p)
prime[i] = true ;
}
}
for ( int p = 2 ; p <= n; p++)
if (prime[p]== false )
allPrimes.add(p);
}
static int factorialDivisors( int n)
{
sieve(n);
int result = 1 ;
for ( int i = 0 ; i < allPrimes.size(); i++)
{
int p = allPrimes.get(i);
int exp = 0 ;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes.get(i);
}
result = result*(( int )Math.pow(allPrimes.get(i), exp+ 1 )- 1 )/
(allPrimes.get(i)- 1 );
}
return result;
}
public static void main(String[] args)
{
System.out.println(factorialDivisors( 4 ));
}
}
|
Python3
allPrimes = [];
def sieve(n):
prime = [ True ] * (n + 1 );
p = 2 ;
while (p * p < = n):
if (prime[p] = = True ):
for i in range (p * 2 , n + 1 , p):
prime[i] = False ;
p + = 1 ;
for p in range ( 2 , n + 1 ):
if (prime[p]):
allPrimes.append(p);
def factorialDivisors(n):
sieve(n);
result = 1 ;
for i in range ( len (allPrimes)):
p = allPrimes[i];
exp = 0 ;
while (p < = n):
exp = exp + int (n / p);
p = p * allPrimes[i];
result = int (result * ( pow (allPrimes[i], exp + 1 ) - 1 ) /
(allPrimes[i] - 1 ));
return result;
print (factorialDivisors( 4 ));
|
C#
using System;
using System.Collections;
class GFG{
static ArrayList allPrimes= new ArrayList();
static void sieve( int n)
{
bool [] prime= new bool [n+1];
for ( int p = 2; p*p <= n; p++)
{
if (prime[p] == false )
{
for ( int i = p*2; i <= n; i += p)
prime[i] = true ;
}
}
for ( int p = 2; p <= n; p++)
if (prime[p]== false )
allPrimes.Add(p);
}
static int factorialDivisors( int n)
{
sieve(n);
int result = 1;
for ( int i = 0; i < allPrimes.Count; i++)
{
int p = ( int )allPrimes[i];
int exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*( int )allPrimes[i];
}
result = result*(( int )Math.Pow(( int )allPrimes[i], exp+1)-1)/
(( int )allPrimes[i]-1);
}
return result;
}
static void Main()
{
Console.WriteLine(factorialDivisors(4));
}
}
|
PHP
<?php
$allPrimes = array ();
function sieve( $n )
{
global $allPrimes ;
$prime = array_fill (0, $n +1,true);
for ( $p = 2; $p * $p <= $n ; $p ++)
{
if ( $prime [ $p ] == true)
{
for ( $i = $p *2; $i <= $n ; $i += $p )
$prime [ $i ] = false;
}
}
for ( $p = 2; $p <= $n ; $p ++)
if ( $prime [ $p ])
array_push ( $allPrimes , $p );
}
function factorialDivisors( $n )
{
global $allPrimes ;
sieve( $n );
$result = 1;
for ( $i = 0; $i < count ( $allPrimes ); $i ++)
{
$p = $allPrimes [ $i ];
$exp = 0;
while ( $p <= $n )
{
$exp = $exp + (int)( $n / $p );
$p = $p * $allPrimes [ $i ];
}
$result = $result *(pow( $allPrimes [ $i ], $exp +1)-1)/
( $allPrimes [ $i ]-1);
}
return $result ;
}
print (factorialDivisors(4));
?>
|
Javascript
<script>
let allPrimes=[];
function sieve(n)
{
let prime = new Array(n + 1);
for (let i = 0; i < n + 1; i++)
prime[i] = true ;
for (let p = 2; p * p <= n; p++)
{
if (prime[p] == true )
{
for (let i = p * 2; i <= n; i += p)
prime[i] = false ;
}
}
for (let p = 2; p <= n; p++)
if (prime[p])
allPrimes.push(p);
}
function factorialDivisors( n)
{
sieve(n);
let result = 1;
for (let i = 0; i < allPrimes.length; i++)
{
let p = allPrimes[i];
let exp = 0;
while (p <= n)
{
exp = exp + Math.floor(n/p);
p = p*allPrimes[i];
}
result = Math.floor(result * (Math.pow(
allPrimes[i], exp + 1) - 1) /
(allPrimes[i] - 1));
}
return result;
}
document.write(factorialDivisors(4));
</script>
|
Output:
60
Time Complexity: O(n*log(log(n)))
Auxiliary Space: O(n)
Last Updated :
13 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...