Given an integer n and a prime number p, find the largest x such that p^{x} (p raised to power x) divides n! (factorial)

**Examples:**

Input: n = 7, p = 3 Output: x = 2 3^{2}divides 7! and 2 is the largest such power of 3. Input: n = 10, p = 3 Output: x = 4 3^{4}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 p^{2}, p^{3}, …

**How many numbers in {1, 2, 3, 4, ….. n} are divisible by p ^{2}, p^{3}, …?**

There are ?n/(p

^{2})? numbers divisible by p

^{2}(Every p

^{2}‘th number would be divisible). Similarly, there are ?n/(p

^{3})? numbers divisible by p

^{3}and so on.

**What is the largest possible value of x?**

So the largest possible power is ?n/p? + ?n/(p^{2})? + ?n/(p^{3})? + ……

Note that we add only ?n/(p^{2})? only once (not twice) as one p is already considered by expression ?n/p?. Similarly, we consider ?n/(p^{3})? (not thrice).

Below is implementation of above idea.

## C++

`// C++ program to find largest x such that p*x divides n! ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns largest power of p that divides n! ` `int` `largestPower(` `int` `n, ` `int` `p) ` `{ ` ` ` `// Initialize result ` ` ` `int` `x = 0; ` ` ` ` ` `// Calculate x = n/p + n/(p^2) + n/(p^3) + .... ` ` ` `while` `(n) ` ` ` `{ ` ` ` `n /= p; ` ` ` `x += n; ` ` ` `} ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10, p = 3; ` ` ` `cout << ` `"The largest power of "` `<< p << ` ` ` `" that divides "` `<< n << ` `"! is "` `<< ` ` ` `largestPower(n, p) << endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C

`// C program to find largest x such that p*x divides n! ` `#include <stdio.h> ` ` ` `// Returns largest power of p that divides n! ` `int` `largestPower(` `int` `n, ` `int` `p) ` `{ ` ` ` `// Initialize result ` ` ` `int` `x = 0; ` ` ` ` ` `// Calculate x = n/p + n/(p^2) + n/(p^3) + .... ` ` ` `while` `(n) ` ` ` `{ ` ` ` `n /= p; ` ` ` `x += n; ` ` ` `} ` ` ` `return` `x; ` `} ` ` ` `// Driver program ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find largest x such that p*x divides n! ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns largest power of p ` ` ` `// that divides n! ` ` ` `static` `int` `Largestpower(` `int` `n, ` `int` `p) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Calculate x = n/p + n/(p^2) + n/(p^3) + .... ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `n /= p; ` ` ` `ans += n; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `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)); ` ` ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find largest ` `# x such that p*x divides n! ` ` ` `# Returns largest power of p that divides n! ` `def` `largestPower(n, p): ` ` ` ` ` `# Initialize result ` ` ` `x ` `=` `0` ` ` ` ` `# Calculate x = n/p + n/(p^2) + n/(p^3) + .... ` ` ` `while` `n: ` ` ` `n ` `/` `=` `p ` ` ` `x ` `+` `=` `n ` ` ` `return` `x ` ` ` `# Driver program ` `n ` `=` `10` `; p ` `=` `3` `print` `(` `"The largest power of %d that divides %d! is %d\n"` `%` ` ` `(p, n, largestPower(n, p))) ` ` ` `# This code is contributed by Shreyanshi Arun. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find largest x ` `// such that p * x divides n! ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` ` ` `// Function that returns largest ` ` ` `// power of p that divides n! ` ` ` `static` `int` `Largestpower(` `int` `n, ` `int` `p) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `ans = 0; ` ` ` ` ` `// Calculate x = n / p + n / (p ^ 2) + ` ` ` `// n / (p ^ 3) + .... ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `n /= p; ` ` ` `ans += n; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` `int` `p = 3; ` ` ` `Console.Write(` `" The largest power of "` `+ p + ` `" that divides "` ` ` `+ n + ` `"! is "` `+ Largestpower(n, p)); ` ` ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find largest ` `// x such that p*x divides n! ` ` ` `// Returns largest power ` `// of p that divides n! ` `function` `largestPower(` `$n` `, ` `$p` `) ` `{ ` ` ` `// Initialize result ` ` ` `$x` `= 0; ` ` ` ` ` `// Calculate x = n/p + ` ` ` `// n/(p^2) + n/(p^3) + .... ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `$n` `= (int)` `$n` `/ ` `$p` `; ` ` ` `$x` `+= ` `$n` `; ` ` ` `} ` ` ` `return` `floor` `(` `$x` `); ` `} ` ` ` `// Driver Code ` `$n` `= 10; ` `$p` `= 3; ` `echo` `"The largest power of "` `, ` `$p` `; ` `echo` `" that divides "` `,` `$n` `, ` `"! is "` `; ` `echo` `largestPower(` `$n` `, ` `$p` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The largest power of 3 that divides 10! is 4

Time complexity of the above solution is Log_{p}n.

**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.

**Source:**

http://e-maxx.ru/algo/factorial_divisors

This article is contributed by **Ankur**. 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.

## Recommended Posts:

- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Section formula (Point that divides a line in given ratio)
- Find a distinct pair (x, y) in given range such that x divides y
- Find the largest composite number that divides N but is strictly lesser than N
- Largest number that divides x and is co-prime with y
- Haversine formula to find distance between two points on a sphere
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Partitions possible such that the minimum element divides all the other elements of the partition
- Program for Stirling Interpolation Formula
- Print first n Fibonacci Numbers using direct formula
- Newton's Divided Difference Interpolation Formula
- Program to implement Inverse Interpolation using Lagrange Formula
- Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula
- Find the last digit when factorial of A divides factorial of B
- Find element in array that divides all array elements
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Find the Smallest number that divides X^X