# 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 **X ^{Y}**

**Examples:**

Input:N = 10, X = 2, Y = 8Output:YESExplanation:

Factorial of 10 is – 3628800

and the value of X^{Y}= 2^{8}= 256

Since, 3628800 is divisible by 256, therefore answer is YES.Input:N = 5, X = 2, Y = 4Output:NOExplanation:

The Factorial of 5 is – 120

and the value of X^{Y}= 2^{4}= 16

Since, 3628800 is not divisible by 16, therefore answer is NO.

**Approach:** The idea is to find the value of N-factorial and X^{Y} separately and then check if the value of N-factorial is divisible X^{Y}.**Algorithm:**

- Calculate the value of N factorial
- Find the value of X
^{Y}. - Check that if the factorial of N is divisible by X
^{Y}.

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

Below is the implementation of the above approach:

## C++

`// CPP implementation 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` |

## 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` |

## Javascript

`<script>` `// JavaScript implementation to check if` `// the value of the N! % X^Y == 0` `function` `check(n,x,y)` `{` ` ` `var` `fact = 1;` ` ` ` ` `// Loop to calculate N-factorial` ` ` `for` `(` `var` `i = 2; i <= n; i++) {` ` ` `fact *= i;` ` ` `}` ` ` `var` `divisor = Math.pow(x, y);` ` ` ` ` `// Condition to check` ` ` `if` `(fact % divisor === 0)` ` ` `document.write(` `"YES"` `);` ` ` `else` ` ` `document.write(` `"NO"` `);` ` ` ` ` `}` ` ` ` ` `var` `n = 10;` ` ` `var` `x = 2;` ` ` `var` `y = 8;` ` ` ` ` `// Function Call` ` ` `check(n, x, y);` `</script>` |

## PHP

`<?php` `// php implementation to check if` `// the value of the N! % X^Y == 0` `function` `check(` `$n` `,` `$x` `,` `$y` `)` `{` ` ` `$fact` `= 1;` ` ` ` ` `// Loop to calculate N-factorial` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `++) {` ` ` `$fact` `*= ` `$i` `;` ` ` `}` ` ` `$divisor` `= pow(` `$x` `, ` `$y` `);` ` ` ` ` `// Condition to check` ` ` `if` `(` `$fact` `% ` `$divisor` `=== 0)` ` ` `echo` `(` `"YES"` `);` ` ` `else` ` ` `echo` `(` `"NO"` `);` ` ` ` ` `}` ` ` ` ` `$n` `= 10;` ` ` `$x` `= 2;` ` ` `$y` `= 8;` ` ` ` ` `// Function Call` ` ` `check(` `$n` `, ` `$x` `, ` `$y` `);` ` ` ` ` `// This code is contributed by _saurabh_jaiswal` `?>` |

**Output:**

YES

**Performance Analysis:**

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**