📜  在n中求素数p的幂!

📅  最后修改于: 2021-04-29 10:22:10             🧑  作者: Mango

给定数字“ n”和质数“ p”。我们需要找出n的素因式分解中’p’的幂!
例子:

Input  : n = 4, p = 2
Output : 3
         Power of 2 in the prime factorization
         of 2 in 4! = 24 is 3

Input  : n = 24, p = 2
Output : 22

天真的方法
天真的方法是在从1到n的每个数字中找到p的幂并将其相加。因为我们知道在乘法过程中会添加幂。

C++
// C++ implementation of finding
// power of p in n!
#include 
 
using namespace std;
 
// Returns power of p in n!
int PowerOFPINnfactorial(int n, int p)
{
    // initializing answer
    int ans = 0;
 
    // initializing
    int temp = p;
 
    // loop until temp<=n
    while (temp <= n) {
 
        // add number of numbers divisible by n
        ans += n / temp;
 
        // each time multiply temp by p
        temp = temp * p;
    }
    return ans;
}
 
// Driver function
int main()
{
    cout << PowerOFPINnfactorial(4, 2) << endl;
    return 0;
}


Java
// Java implementation of naive approach
 
public class GFG
{
    // Method to calculate the power of prime number p in n!
    static int PowerOFPINnfactorial(int n, int p)
    {
        // initializing answer
        int ans = 0;
      
        // finding power of p from 1 to n
        for (int i = 1; i <= n; i++) {
      
            // variable to note the power of p in i
            int count = 0, temp = i;
      
            // loop until temp is equal to i
            while (temp % p == 0) {
                count++;
                temp = temp / p;
            }
      
            // adding count to i
            ans += count;
        }
        return ans;
    }
     
    // Driver Method
    public static void main(String[] args)
    {
        System.out.println(PowerOFPINnfactorial(4, 2));
    }
}


Python3
# Python3 implementation of
# finding power of p in n!
 
# Returns power of p in n!
def PowerOFPINnfactorial(n, p):
 
    # initializing answer
    ans = 0;
 
    # initializing
    temp = p;
 
    # loop until temp<=n
    while (temp <= n):
 
        # add number of numbers
        # divisible by n
        ans += n / temp;
 
        # each time multiply
        # temp by p
        temp = temp * p;
         
    return ans;
 
# Driver Code
print(PowerOFPINnfactorial(4, 2));
 
# This code is contributed by
# mits


C#
// C# implementation of naive approach
using System;
 
public class GFG
{
    // Method to calculate power
    // of prime number p in n!
    static int PowerOFPINnfactorial(int n, int p)
    {
        // initializing answer
        int ans = 0;
     
        // finding power of p from 1 to n
        for (int i = 1; i <= n; i++) {
     
            // variable to note the power of p in i
            int count = 0, temp = i;
     
            // loop until temp is equal to i
            while (temp % p == 0) {
                count++;
                temp = temp / p;
            }
     
            // adding count to i
            ans += count;
        }
        return ans;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        Console.WriteLine(PowerOFPINnfactorial(4, 2));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ implementation of finding power of p in n!
#include 
 
using namespace std;
 
// Returns power of p in n!
int PowerOFPINnfactorial(int n, int p)
{
    // initializing answer
    int ans = 0;
 
    // initializing
    int temp = p;
 
    // loop until temp<=n
    while (temp <= n) {
 
        // add number of numbers divisible by n
        ans += n / temp;
 
        // each time multiply temp by p
        temp = temp * p;
    }
    return ans;
}
 
// Driver function
int main()
{
    cout << PowerOFPINnfactorial(4, 2) << endl;
    return 0;
}


Java
// Java implementation of finding power of p in n!
 
public class GFG
{
    // Method to calculate the power of prime number p in n!
    static int PowerOFPINnfactorial(int n, int p)
    {
        // initializing answer
        int ans = 0;
      
        // initializing
        int temp = p;
      
        // loop until temp<=n
        while (temp <= n) {
      
            // add number of numbers divisible by n
            ans += n / temp;
      
            // each time multiply temp by p
            temp = temp * p;
        }
        return ans;
    }
     
    // Driver Method
    public static void main(String[] args)
    {
        System.out.println(PowerOFPINnfactorial(4, 2));
    }
}


Python3
# Python3 implementation of
# finding power of p in n!
 
# Returns power of p in n!
def PowerOFPINnfactorial(n, p):
 
    # initializing answer
    ans = 0
 
    # initializing
    temp = p
 
    # loop until temp<=n
    while (temp <= n) :
 
        # add number of numbers
        # divisible by n
        ans += n / temp
 
        # each time multiply
        # temp by p
        temp = temp * p
     
    return int(ans)
 
# Driver Code
print(PowerOFPINnfactorial(4, 2))
 
# This code is contributed
# by Smitha


C#
// C# implementation of finding
// power of p in n!
using System;
 
public class GFG
{
 
    // Method to calculate power
    // of prime number p in n!
    static int PowerOFPINnfactorial(int n, int p)
    {
        // initializing answer
        int ans = 0;
     
        // initializing
        int temp = p;
     
        // loop until temp <= n
        while (temp <= n) {
     
            // add number of numbers divisible by n
            ans += n / temp;
     
            // each time multiply temp by p
            temp = temp * p;
        }
        return ans;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        Console.WriteLine(PowerOFPINnfactorial(4, 2));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

3

高效方法
在讨论有效方法之前,让我们讨论一个给定的数字n,即从1到n有多少个数字可被m整除的问题,答案是floor(n / m)。
现在回到我们原来的问题,以求p in n的幂!我们要做的是计算从1到n的可被p整除的数目,然后被p^2  然后p^3  直到p^k  > n并添加它们。这将是我们所需的答案。

Powerofp(n!) = floor(n/p) + floor(n/p^2) + floor(n/p^3)........ 

以下是上述步骤的执行。

C++

// C++ implementation of finding power of p in n!
#include 
 
using namespace std;
 
// Returns power of p in n!
int PowerOFPINnfactorial(int n, int p)
{
    // initializing answer
    int ans = 0;
 
    // initializing
    int temp = p;
 
    // loop until temp<=n
    while (temp <= n) {
 
        // add number of numbers divisible by n
        ans += n / temp;
 
        // each time multiply temp by p
        temp = temp * p;
    }
    return ans;
}
 
// Driver function
int main()
{
    cout << PowerOFPINnfactorial(4, 2) << endl;
    return 0;
}

Java

// Java implementation of finding power of p in n!
 
public class GFG
{
    // Method to calculate the power of prime number p in n!
    static int PowerOFPINnfactorial(int n, int p)
    {
        // initializing answer
        int ans = 0;
      
        // initializing
        int temp = p;
      
        // loop until temp<=n
        while (temp <= n) {
      
            // add number of numbers divisible by n
            ans += n / temp;
      
            // each time multiply temp by p
            temp = temp * p;
        }
        return ans;
    }
     
    // Driver Method
    public static void main(String[] args)
    {
        System.out.println(PowerOFPINnfactorial(4, 2));
    }
}

Python3

# Python3 implementation of
# finding power of p in n!
 
# Returns power of p in n!
def PowerOFPINnfactorial(n, p):
 
    # initializing answer
    ans = 0
 
    # initializing
    temp = p
 
    # loop until temp<=n
    while (temp <= n) :
 
        # add number of numbers
        # divisible by n
        ans += n / temp
 
        # each time multiply
        # temp by p
        temp = temp * p
     
    return int(ans)
 
# Driver Code
print(PowerOFPINnfactorial(4, 2))
 
# This code is contributed
# by Smitha

C#

// C# implementation of finding
// power of p in n!
using System;
 
public class GFG
{
 
    // Method to calculate power
    // of prime number p in n!
    static int PowerOFPINnfactorial(int n, int p)
    {
        // initializing answer
        int ans = 0;
     
        // initializing
        int temp = p;
     
        // loop until temp <= n
        while (temp <= n) {
     
            // add number of numbers divisible by n
            ans += n / temp;
     
            // each time multiply temp by p
            temp = temp * p;
        }
        return ans;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        Console.WriteLine(PowerOFPINnfactorial(4, 2));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

3