Given an integer n and a prime number p, find the largest x such that px (p raised to power x) divides n! (factorial) Examples:
Input: n = 7, p = 3
Output: x = 2
32 divides 7! and 2 is the largest such power of 3.
Input: n = 10, p = 3
Output: x = 4
34 divides 10! and 4 is the largest such power of 3.
n! is multiplication of {1, 2, 3, 4, …n}.How many numbers in {1, 2, 3, 4, ….. n} are divisible by p? Every p’th number is divisible by p in {1, 2, 3, 4, ….. n}. Therefore in n!, there are ?n/p? numbers divisible by p. So we know that the value of x (largest power of p that divides n!) is at-least ?n/p?. Can x be larger than ?n/p? ? Yes, there may be numbers which are divisible by p2 , p3 , … How many numbers in {1, 2, 3, 4, ….. n} are divisible by p2 , p3 , …? There are ?n/(p2 )? numbers divisible by p2 (Every p2 ‘th number would be divisible). Similarly, there are ?n/(p3 )? numbers divisible by p3 and so on.What is the largest possible value of x? So the largest possible power is ?n/p? + ?n/(p2 )? + ?n/(p3 )? + …… Note that we add only ?n/(p2 )? only once (not twice) as one p is already considered by expression ?n/p?. Similarly, we consider ?n/(p3 )? (not thrice). Below is the implementation of the above idea.
C++
#include <iostream>
using namespace std;
int largestPower(int n, int p)
{
int x = 0;
while (n)
{
n /= p;
x += n;
}
return x;
}
int main()
{
int n = 10, p = 3;
cout << "The largest power of "<< p <<
" that divides " << n << "! is "<<
largestPower(n, p) << endl;
return 0;
}
C
#include <stdio.h>
int largestPower(int n, int p)
{
int x = 0;
while (n)
{
n /= p;
x += n;
}
return x;
}
int main()
{
int n = 10, p = 3;
printf("The largest power of %d that divides %d! is %d\n",
p, n, largestPower(n, p));
return 0;
}
Java
import java.io.*;
class GFG
{
static int Largestpower(int n, int p)
{
int ans = 0;
while (n > 0)
{
n /= p;
ans += n;
}
return ans;
}
public static void main (String[] args)
{
int n = 10;
int p = 3;
System.out.println(" The largest power of " + p + " that divides "
+ n + "! is " + Largestpower(n, p));
}
}
Python3
def largestPower(n, p):
x = 0
while n:
n /= p
x += n
return x
n = 10; p = 3
print ("The largest power of %d that divides %d! is %d\n"%
(p, n, largestPower(n, p)))
C#
using System;
public class GFG
{
static int Largestpower(int n, int p)
{
int ans = 0;
while (n > 0)
{
n /= p;
ans += n;
}
return ans;
}
public static void Main ()
{
int n = 10;
int p = 3;
Console.Write(" The largest power of " + p + " that divides "
+ n + "! is " + Largestpower(n, p));
}
}
PHP
<?php
function largestPower($n, $p)
{
$x = 0;
while ($n)
{
$n = (int)$n / $p;
$x += $n;
}
return floor($x);
}
$n = 10;
$p = 3;
echo "The largest power of ", $p ;
echo " that divides ",$n , "! is ";
echo largestPower($n, $p);
?>
Javascript
<script>
function largestPower(n, p)
{
let x = 0;
while (n)
{
n = parseInt(n / p);
x += n;
}
return Math.floor(x);
}
let n = 10;
let p = 3;
document.write("The largest power of " + p);
document.write(" that divides " + n + "! is ");
document.write(largestPower(n, p));
</script>
Output
The largest power of 3 that divides 10! is 4
Time complexity: O(logp n)Auxiliary Space: O(1)What to do if p is not prime? We can find all prime factors of p and compute result for every prime factor. Refer Largest power of k in n! (factorial) where k may not be prime for details.
Approach 2: Using recursion Another way to implement Legendre’s formula is to use recursion. Here is the code implementation:
Start the program by including the required header files and the standard namespace.
Define a function named largestPower that takes two integers n and p as input and returns an integer.
Check if n is equal to 0. If it is, return 0 as the largest power of p that divides n! is 0.
Calculate the largest power of p that divides n! recursively by dividing n by p and adding the quotient to the result of largestPower(n/p, p).
Return the result of the calculation in step 4.
Define the main function, which sets n equal to 10 and p equal to 3, calls the largestPower function with these values, and outputs the result.
End the program by returning 0 from the main function.
C++
#include<bits/stdc++.h>
using namespace std;
int largestPower(int n, int p) {
if (n == 0) {
return 0;
}
return n/p + largestPower(n/p, p);
}
int main() {
int n = 10, p = 3;
cout << "The largest power of " << p << " that divides " << n << "! is " << largestPower(n, p) << endl;
return 0;
}
Java
public class Main {
public static int largestPower(int n, int p) {
if (n == 0) {
return 0;
}
return n / p + largestPower(n / p, p);
}
public static void main(String[] args) {
int n = 10, p = 3;
System.out.printf("The largest power of %d that divides %d! is %d", p, n, largestPower(n, p));
}
}
Python3
def largestPower(n, p):
if n == 0:
return 0
return n // p + largestPower(n // p, p)
n, p = 10, 3
print(f"The largest power of {p} that divides {n}! is {largestPower(n, p)}")
C#
using System;
public class MainClass {
public static int largestPower(int n, int p) {
if (n == 0) {
return 0;
}
return n / p + largestPower(n / p, p);
}
public static void Main(string[] args) {
int n = 10, p = 3;
Console.WriteLine("The largest power of {0} that divides {1}! is {2}", p, n, largestPower(n, p));
}
}
Javascript
function largestPower(n, p) {
if (n == 0) {
return 0;
}
let x=Math.floor(n/p);
return x + largestPower(x, p);
}
let n = 10, p = 3;
document.write("The largest power of " + p + " that divides " + n + "! is " + largestPower(n, p));
Output
The largest power of 3 that divides 10! is 4
Time complexity: O(logpn)
The time complexity of the largestPower function is O(log_p(n)), where n is the input integer and p is the divisor. This is because the function divides n by p in each recursive call, which reduces the size of n by a factor of p. The number of times the function can be called before n becomes less than p is proportional to the logarithm base p of n. Therefore, the time complexity is O(log_p(n)).
Auxiliary Space: O(logpn)
The space complexity of the function is O(log_p(n)), because each recursive call to the function adds a new stack frame to the call stack, and the depth of the call stack is proportional to the number of times the function is called, which is proportional to the logarithm base p of n. Therefore, the space complexity is O(log_p(n)).Source: http://e-maxx.ru/algo/factorial_divisors Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
14 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...