Count Divisors of Factorial
Last Updated :
13 Feb, 2023
Given a number n, count the total number of divisors of n!.
Examples:
Input : n = 4
Output: 8
Explanation:
4! is 24. Divisors of 24 are 1, 2, 3, 4, 6,
8, 12 and 24.
Input : n = 5
Output : 16
Explanation:
5! is 120. Divisors of 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24 30, 40, 60 and 12
A Simple Solution is to first compute the factorial of the given number, then count the number of divisors of the factorial. This solution is not efficient and may cause overflow due to factorial computation.
A better solution is based on Legendre’s formula. Below are the step:
- 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 value of largest power that divides n! is ?n/p? + ?n/(p2)? + ?n/(p3)? + ……
Let these values be exp1, exp2, exp3,… Using the above formula, we get the 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.
- The result is (exp1 + 1) * (exp2 + 1) * (exp3 + 1) … for all prime numbers, For n = 6, the values exp1, exp2, and exp3 are 4 2 and 1 respectively (computed in above step 2). So our result is (4 + 1)*(2 + 1) * (1 + 1) = 30
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
vector<ull> 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);
}
ull factorialDivisors(ull n)
{
sieve(n);
ull result = 1;
for ( int i=0; i < allPrimes.size(); i++)
{
ull p = allPrimes[i];
ull exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes[i];
}
result = result*( exp +1);
}
return result;
}
int main()
{
cout << factorialDivisors(6);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector<Integer> allPrimes= new Vector<Integer>();
static void sieve( int n){
boolean []prime= new boolean [n+ 1 ];
for ( int i= 0 ;i<=n;i++)
prime[i]= 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.add(p);
}
static long factorialDivisors( int n)
{
sieve(n);
long result = 1 ;
for ( int i= 0 ; i < allPrimes.size(); i++)
{
long p = allPrimes.get(i);
long exp = 0 ;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes.get(i);
}
result = result*(exp+ 1 );
}
return result;
}
public static void main(String []args)
{
System.out.println(factorialDivisors( 6 ));
}
}
|
Python3
allPrimes = [];
def sieve(n):
prime = [ True ] * (n + 1 );
p = 2 ;
while (p * p < = n):
if (prime[p] = = True ):
i = p * 2 ;
while (i < = n):
prime[i] = False ;
i + = p;
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 = result * (exp + 1 );
return result;
print (factorialDivisors( 6 ));
|
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 i = 0; i <= n; i++)
prime[i] = 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.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 * (exp + 1);
}
return result;
}
public static void Main()
{
Console.WriteLine(factorialDivisors(6));
}
}
|
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 * ( $exp + 1);
}
return $result ;
}
echo factorialDivisors(6);
?>
|
Javascript
<script>
let allPrimes = [];
function sieve(n)
{
let prime = new Array(n+1);
for (let i = 0; i <= n; 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 + parseInt(n / p, 10);
p = p * allPrimes[i];
}
result = result * (exp + 1);
}
return result;
}
document.write(factorialDivisors(6));
</script>
|
Time Complexity: (O(n * log(logn))
Auxiliary Space: O(n)
This article is reviewed by team GeeksforGeeks.
Share your thoughts in the comments
Please Login to comment...