Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)

2.5

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).
Following is implementation based on above idea.

C/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;
}

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


Output:
The largest power of 3 that divides 10! is 4

Time complexity of the above solution is Logpn.

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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.5 Average Difficulty : 2.5/5.0
Based on 8 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.