📜  N 可以除以素数的不同幂的最大次数

📅  最后修改于: 2022-05-13 01:56:07.978000             🧑  作者: Mango

N 可以除以素数的不同幂的最大次数

给定一个整数N ,任务是计算N可以除以整数K的最大次数,其中K是质数整数的幂,并且 K 的值总是不同的。

例子:

方法:给定的问题可以使用贪心方法来解决。这个想法是将数字除以其所有主要因素,按其价值和权力的升序排列。使用[2, √N]范围内的变量i进行迭代,如果i整除N ,则将Ni的递增幂(即 1、2、3…)整除,直到它可以被它整除。保持变量中除数的计数,这是所需的答案。

下面是上述方法的实现:

C++
// C++ program of th above approach
#include 
using namespace std;
 
// Program to find maximum number of
// times N can be divided by distinct
// powers of prime integers
int maxDivisions(int N)
{
    // Stores the required count
    int cnt = 0;
 
    int range = sqrt(N);
 
    // Loop to iterate in range [2, √N]
    for (int i = 2; i <= range; i++) {
 
        // If i divides N
        if (N % i == 0) {
            int j = i;
 
            // Divide N with increasing
            // powers of i
            while (N % j == 0 && N) {
                N = N / j;
                // Update j
                j *= i;
 
                // Increment cnt
                cnt++;
            }
 
            // Remove the remaining power
            // of i to avoid repetition
            while (N % i == 0) {
                N /= i;
            }
        }
    }
 
    // Return Answer
    return cnt;
}
 
// Driver Code
int main()
{
    int N = 100;
    cout << maxDivisions(N);
    return 0;
}


Java
// JAVA program of th above approach
import java.util.*;
class GFG
{
 
  // Program to find maximum number of
  // times N can be divided by distinct
  // powers of prime integers
  public static int maxDivisions(int N)
  {
 
    // Stores the required count
    int cnt = 0;
 
    double range = Math.sqrt(N);
 
    // Loop to iterate in range [2, √N]
    for (int i = 2; i <= range; i++) {
 
      // If i divides N
      if (N % i == 0) {
        int j = i;
 
        // Divide N with increasing
        // powers of i
        while (N % j == 0 && N != 0) {
          N = N / j;
          // Update j
          j *= i;
 
          // Increment cnt
          cnt++;
        }
 
        // Remove the remaining power
        // of i to avoid repetition
        while (N % i == 0) {
          N /= i;
        }
      }
    }
 
    // Return Answer
    return cnt;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 100;
    System.out.print(maxDivisions(N));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach
from math import ceil, sqrt
 
# Program to find maximum number of
# times N can be divided by distinct
# powers of prime integers
def maxDivisions(N):
 
    # Stores the required count
    cnt = 0;
 
    _range = ceil(sqrt(N));
 
    # Loop to iterate in range [2, √N]
    for i in range(2, _range + 1):
 
        # If i divides N
        if (N % i == 0):
            j = i;
 
            # Divide N with increasing
            # powers of i
            while (N % j == 0 and N):
                N = N / j;
                # Update j
                j *= i;
 
                # Increment cnt
                cnt += 1
 
            # Remove the remaining power
            # of i to avoid repetition
            while (N % i == 0):
                N /= i;
             
    # Return Answer
    return cnt;
 
# Driver Code
N = 100;
print(maxDivisions(N));
 
# This code is contributed by gfgking


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
 
// Program to find maximum number of
  // times N can be divided by distinct
  // powers of prime integers
  public static int maxDivisions(int N)
  {
 
    // Stores the required count
    int cnt = 0;
 
    double range = Math.Sqrt(N);
 
    // Loop to iterate in range [2, √N]
    for (int i = 2; i <= range; i++) {
 
      // If i divides N
      if (N % i == 0) {
        int j = i;
 
        // Divide N with increasing
        // powers of i
        while (N % j == 0 && N != 0) {
          N = N / j;
          // Update j
          j *= i;
 
          // Increment cnt
          cnt++;
        }
 
        // Remove the remaining power
        // of i to avoid repetition
        while (N % i == 0) {
          N /= i;
        }
      }
    }
 
    // Return Answer
    return cnt;
  }
 
// Driver Code
public static void Main()
{
    int N = 100;
    Console.Write(maxDivisions(N));
}
}
 
// This code is contributed by sanjoy_62.


Javascript



输出
2

时间复杂度: O(√N)
辅助空间: O(1)