Legendre's formula - Largest power of a prime p in n!
Last Updated :
04 Mar, 2025
Given an integer n and a prime number p, the task is to find the largest x such that px (p raised to power x) divides n!.
Examples:
Input: n = 7, p = 3
Output: x = 2
Explanation: 32 divides 7! and 2 is the largest such power of 3.
Input: n = 10, p = 3
Output: x = 4
Explanation: 34 divides 10! and 4 is the largest such power of 3.
We know that n! is the product of all the integers from 1 to n. For each multiple of p in the range [1, n], we know that we get at least one factor of p. Therefore in n!, there are at least floor(n!/p) integers divisible by p. Furthermore, for each multiple of p2, we get one more factor of p. Similarly, for each multiple of p3, we get another extra factor of p. So, the largest value of x is the sum of total number of factors, that is floor(n/p) + floor(n/p2) + floor(n/p3) ... and so on till the floor value reaches 0.
Legendre's Formula:
Largest exponent of p in n! = Sum of (floor(n/(pi)), where i = 1, 2, 3, 4..... and so on.
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) {
int res = 0;
// Calculate res = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
res += n;
}
return res;
}
int main() {
int n = 10, p = 3;
cout << largestPower(n, p) << endl;
return 0;
}
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) {
int res = 0;
// Calculate res = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
res += n;
}
return res;
}
int main() {
int n = 10, p = 3;
printf("%d\n", largestPower(n, p));
return 0;
}
Java
// Java program to find largest x such that p^x divides n!
class GfG {
// Returns largest power of p that divides n!
static int largestPower(int n, int p) {
int res = 0;
// Calculate res = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
res += n;
}
return res;
}
public static void main(String[] args) {
int n = 10, p = 3;
System.out.println(largestPower(n, p));
}
}
Python
# Python program to find largest x such that p^x divides n!
def largestPower(n, p):
res = 0
# Calculate res = n/p + n/(p^2) + n/(p^3) + ....
while n > 0:
n //= p
res += n
return res
if __name__ == "__main__":
n = 10
p = 3
print(largestPower(n, p))
C#
// C# program to find largest x such that p^x divides n!
using System;
class GfG {
// Returns largest power of p that divides n!
static int largestPower(int n, int p) {
int res = 0;
// Calculate res = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
res += n;
}
return res;
}
static void Main(string[] args) {
int n = 10, p = 3;
Console.WriteLine(largestPower(n, p));
}
}
JavaScript
// JavaScript program to find largest x such that p^x divides n!
function largestPower(n, p) {
let res = 0;
// Calculate res = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n = Math.floor(n / p);
res += n;
}
return res;
}
const n = 10, p = 3;
console.log(largestPower(n, p));
Time complexity: O(logpn), as in each iteration we are reducing n to n/p.
Auxiliary Space: O(1)
Since we are repeatedly counting the factors of p and keep reducing the value of n, we can also use recursion to solve the problem. The recurrence relation will be:
largestPower(n, p) = n/p + largestPower(n/p, p)
The base case will be when n = 0, we can return the count of factors as 0.
C++
// Recursive C++ program to find largest x such that
// p^x divides n!
#include<iostream>
using namespace std;
int largestPower(int n, int p) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return n/p + largestPower(n/p, p);
}
int main() {
int n = 10, p = 3;
cout << largestPower(n, p);
return 0;
}
C
// Recursive C program to find largest x such that
// p^x divides n!
#include <stdio.h>
int largestPower(int n, int p) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return n / p + largestPower(n / p, p);
}
int main() {
int n = 10, p = 3;
printf("%d", largestPower(n, p));
return 0;
}
Java
// Recursive Java program to find largest x such that
// p^x divides n!
class GfG {
static int largestPower(int n, int p) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return n / p + largestPower(n / p, p);
}
public static void main(String[] args) {
int n = 10, p = 3;
System.out.println(largestPower(n, p));
}
}
Python
# Recursive Python program to find largest x such that
# p^x divides n!
def largestPower(n, p):
# Base Case
if n == 0:
return 0
# Recursive Case
return n // p + largestPower(n // p, p)
if __name__ == "__main__":
n = 10
p = 3
print(largestPower(n, p))
C#
// Recursive C# program to find largest x such that
// p^x divides n!
using System;
class GfG {
static int largestPower(int n, int p) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return n / p + largestPower(n / p, p);
}
static void Main(string[] args) {
int n = 10, p = 3;
Console.WriteLine(largestPower(n, p));
}
}
JavaScript
// Recursive JavaScript program to find largest x such that
// p^x divides n!
function largestPower(n, p) {
// Base Case
if (n === 0)
return 0;
// Recursive Case
return Math.floor(n / p) + largestPower(Math.floor(n / p), p);
}
const n = 10, p = 3;
console.log(largestPower(n, p));
Time complexity: O(logpn), as in each recursive call we are reducing n to n/p.
Auxiliary Space: O(logpn), as we are using recursive call stack.
Similar Reads
Factorial of a Number Given the number n (n >=0), find its factorial. Factorial of n is defined as 1 x 2 x ... x n. For n = 0, factorial is 1. We are going to discuss iterative and recursive programs in this post.Examples:Input: n = 5Output: 120Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120Input: n = 4Output: 24Explanation
7 min read
Legendre's formula - Largest power of a prime p in n! Given an integer n and a prime number p, the task is to find the largest x such that px (p raised to power x) divides n!.Examples: Input: n = 7, p = 3Output: x = 2Explanation: 32 divides 7! and 2 is the largest such power of 3.Input: n = 10, p = 3Output: x = 4Explanation: 34 divides 10! and 4 is the
6 min read
Count trailing zeroes in factorial of a number Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5Output: 1 Explanation: Factorial of 5 is 120 which has one trailing 0.Input: n = 20Output: 4Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.Input: n = 100Output: 2
8 min read
Find the Factorial of a large number Factorial of a non-negative integer, is the multiplication of all integers smaller than or equal to n. Factorial of a numberExamples: Input: 100Output: 933262154439441526816992388562667004- 907159682643816214685929638952175999- 932299156089414639761565182862536979- 2082722375825118521091686400000000
15+ min read
Primorial of a number Given a number n, the task is to calculate its primorial. Primorial (denoted as Pn#) is a product of first n prime numbers. Primorial of a number is similar to the factorial of a number. In primorial, not all the natural numbers get multiplied only prime numbers are multiplied to calculate the primo
10 min read
Find maximum power of a number that divides a factorial Given two numbers, fact and n, find the largest power of n that divides fact! (Factorial of fact). Examples:Â Input : fact = 5, n = 2 Output : 3 Explanation: Value of 5! is 120. The largest power of 2 that divides 120 is 8 (or 23 Input : fact = 146, n = 15 Output : 35 The idea is based on Legendreâs
12 min read
Largest power of k in n! (factorial) where k may not be prime Given two positive integers k and n, where k > 1, find the largest power of k that divides n! (n factorial).Examples: Input: n = 7, k = 2Output: 4Explanation: 7! = 5040, and 24 = 16 is the highest power of 2 that divides 5040.Input: n = 10, k = 9Output: 2Explanation: 10! = 3628800, and 9² = 81 is
15+ min read
Check if a number is a Krishnamurthy Number or not A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.For example, 145 is the sum of the factorial of each digit.1! + 4! + 5! = 1 + 24 + 120 = 145 Examples: Input : 145Output : YESExplanation: 1! + 4! + 5! = 1 + 24 + 120 = 145, which is equal to input,
10 min read
Last non-zero digit of a factorial Given a number n, find the last non-zero digit in n!.Examples: Input : n = 5 Output : 2 5! = 5 * 4 * 3 * 2 * 1 = 120 Last non-zero digit in 120 is 2. Input : n = 33 Output : 8 Recommended PracticeLast non-zero digit in factorialTry It! A Simple Solution is to first find n!, then find the last non-ze
14 min read
Count digits in a factorial using Logarithm Given an integer N, find the number of digits that appear in its factorial, where factorial is defined as, factorial(n) = 1*2*3*4........*n and factorial(0) = 1Examples : Input: 5Output: 3Explanation: 5! = 120, i.e., 3 digitsInput: 10Output: 7Explanation: 10! = 3628800, i.e., 7 digits Naive approach
8 min read