📌  相关文章
📜  勒让德勒的公式(给出p和n,找到最大的x,以使p ^ x除以n!)

📅  最后修改于: 2021-04-27 18:45:20             🧑  作者: Mango

给定一个整数n和一个质数p,找到最大的x,以使p x (p的幂次幂)除以n! (阶乘)
例子:

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!是{1,2,3,4,… n}的乘法。
{1、2、3、4 ….. n}中有多少个数字可以被p整除?
在第{1,2,3,4,….. n}个中,第p个数字可以被p整除。因此,在n!中,存在可以被p整除的⌊n/p⌋个数字。因此我们知道x的值(除以n的p的最大幂)至少为⌊n/p⌋。
x可以大于⌊n/p⌋吗?
是的,可能存在可以被p 2 ,p 3 …整除的数字。
{1,2,3,4,….. n}中有多少个数字可以被p 2 ,p 3 ,…整除?
有可以被p 2整除的⌊n/(p 2 )⌋个数字(每个p 2 ‘个数都可以被整除)。类似地,存在可被p 3整除的⌊n/(p 3 )⌋个数字,依此类推。
x的最大可能值是多少?
因此,最大可能乘方为⌊n/p⌋+⌊n/(p 2 )⌋+⌊n/(p 3 )⌋+……
请注意,由于表达式⌊n/p⌋已经考虑了一个p,因此我们仅将⌊n/(p 2 )⌋仅添加了一次(不是两次)。同样,我们考虑⌊n/(p 3 )⌋(不是三次)。
以下是上述想法的实现。

C++
// C++ program to find largest x such that p*x divides n!
#include 
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


C
// C program to find largest x such that p*x divides n!
#include 
 
// 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));
         
         
    }
}


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.


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


PHP


Javascript


输出:

The largest power of 3 that divides 10! is 4