Open In App

Legendre's formula - Largest power of a prime p in n!

Last Updated : 04 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

Using Legendre's Formula - Iterative Approach

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));

Output
4

Time complexity: O(logpn), as in each iteration we are reducing n to n/p.
Auxiliary Space: O(1)

Using Legendre's Formula - Recursive Approach

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));

Output
4

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.


Next Article

Similar Reads