# Check if N-factorial is divisible by X^Y

• Last Updated : 12 May, 2021

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++

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

## Javascript

 ``

## PHP

 ``

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.

My Personal Notes arrow_drop_up