Check if N-factorial is divisible by X^Y

Given three integers N, X and Y, the task is to check that if N! is divisible by XY

Examples:

Input: N = 10, X = 2, Y = 8
Output: YES
Explanation:
Factorial of 10 is – 3628800
and the value of XY = 28 = 256
Since, 3628800 is divisible by 256, therefore answer is YES.



Input: N = 5, X = 2, Y = 4
Output: NO
Explanation:
The Factorial of 5 is – 120
and the value of XY = 24 = 16
Since, 3628800 is not divisible by 16, therefore answer is NO.

Approach: The idea is to find the value of N-factorial and XY separately and then check if the value of N-factorial is divisible XY.

Algorithm:

Note: This approach does not work for large values of N.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implmentation to check if
// the value of the N! % X^Y == 0
#include<bits/stdc++.h>
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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implmentation 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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implmentation 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implmentation 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

chevron_right



Output:

YES

Performance Analysis:

  • Time Complexity: O(N)
  • Auxiliary Space: O(1).

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.