📜  检查N阶乘是否可被X ^ Y整除

📅  最后修改于: 2021-04-29 09:20:33             🧑  作者: Mango

给定三个整数N,X和Y,任务是检查是否为N!可被X Y整除

例子:

方法:想法是分别找到N阶乘和X Y的值,然后检查N阶乘的值是否可整除X Y。

算法:

  • 计算N阶乘的值
  • 找到X Y的值。
  • 检查N的阶乘是否可被X Y整除。

注意:此方法不适用于较大的N值。

下面是上述方法的实现:

C++
// CPP implementation to check if
// the value of the N! % X^Y == 0
#include
using namespace std;
  
      
    // Function to check if N! % X^Y == 0
    void check(int n,int x, int y){
        int fact = 1;
          
        // Loop to calculate N-factorial
        for (int i = 2; i <= n; i++) {
            fact *= i;
        }
  
        int divisor = pow(x, y);
          
        // Condition to check
        if (fact % divisor == 0)
            cout << "YES";
        else
            cout << "NO";
          
    }
      
    // Driver Code
        int main()
    {
        int n = 10;
        int x = 2;
        int y = 8;
          
        // Function Call
        check(n, x, y);
    }
  
// This code is contributed by Surendra_Gangwar


Java
// Java implementation to check if
// the value of the N! % X^Y == 0
import java.util.*;
import java.lang.*;
  
class divisible {
      
    // Function to check if N! % X^Y == 0
    public static void check(int n, 
                         int x, int y){
        long fact = 1;
          
        // Loop to calculate N-factorial
        for (int i = 2; i <= n; i++) {
            fact *= i;
        }
  
        long divisor = (long)Math.pow(x, y);
          
        // Condition to check
        if (fact % divisor == 0)
            System.out.println("YES");
        else
            System.out.println("NO");
          
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        int x = 2;
        int y = 8;
          
        // Function Call
        check(n, x, y);
    }
}


Python3
# Python3 implementation to check if 
# the value of the N! % X^Y == 0
      
# Function to check if N! % X^Y == 0 
def check(n, x, y) :
    fact = 1;
      
    # Loop to calculate N-factorial
    for i in range(2, n + 1) :
        fact *= i;
    divisor = x ** y;
          
    # Condition to check
    if (fact % divisor == 0) :
        print("YES");
    else :
        print("NO"); 
  
# Driver Code 
if __name__ == "__main__" : 
      
    n = 10; 
    x = 2; 
    y = 8; 
          
    # Function Call 
    check(n, x, y);
  
# This code is contributed by Yash_R


C#
// C# implementation to check if
// the value of the N! % X^Y == 0
using System;
  
class divisible {
       
    // Function to check if N! % X^Y == 0
    public static void check(int n, 
                         int x, int y){
        long fact = 1;
           
        // Loop to calculate N-factorial
        for (int i = 2; i <= n; i++) {
            fact *= i;
        }
   
        long divisor = (long)Math.Pow(x, y);
           
        // Condition to check
        if (fact % divisor == 0)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
           
    }
       
    // Driver Code
    public static void Main(String []args)
    {
        int n = 10;
        int x = 2;
        int y = 8;
           
        // Function Call
        check(n, x, y);
    }
}
  
// This code is contributed by 29AjayKumar


输出:

YES

性能分析:

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