Given two integers, **X** and **K**, the task is to find if **X ^{2}** is divisible by

**K**or not. Here, both

**K**and

**X**can lie in the range

**[1,10**.

^{18}]**Examples:**

Input:X = 6, K = 9

Output:YES

Explanation:

Since 6^{2}is equal to 36, which is divisible by 9.

Input:X = 7, K = 21

Output:NO

Explanation:

Since 7^{2}is equal to 49, which is not divisible by 21.

**Approach:**

As mentioned above, **X** can lie in the range **[1,10 ^{18}]**, so we can not directly check if

**X**is divisible by

^{2}**K**or not as it can cause a memory overflow. Hence the efficient approach is to first calculate the Greatest Common Divisor of

**X**and

**K**. After the GCD is calculated, we will take it as the maximum portion which we can divide from

**X**and

**K**. Reduce K by GCD and check if

**X**is divisible by the reduced

**K**or not.

Below is the implementation of above approach:

## C++

`// C++ implementation to ` `// check if the square of X is ` `// divisible by K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return if ` `// square of X is divisible ` `// by K ` `void` `checkDivisible(` `int` `x, ` `int` `k) ` `{ ` ` ` `// Finding gcd of x and k ` ` ` `int` `g = __gcd(x, k); ` ` ` ` ` `// Dividing k by their gcd ` ` ` `k /= g; ` ` ` ` ` `// Check for divisibility of X ` ` ` `// by reduced K ` ` ` `if` `(x % k == 0) { ` ` ` `cout << ` `"YES\n"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"NO\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `x = 6, k = 9; ` ` ` `checkDivisible(x, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to ` `// check if the square of X is ` `// divisible by K ` ` ` `class` `GFG{ ` ` ` `// Function to return if ` `// square of X is divisible ` `// by K ` `static` `void` `checkDivisible(` `int` `x, ` `int` `k) ` `{ ` ` ` `// Finding gcd of x and k ` ` ` `int` `g = __gcd(x, k); ` ` ` ` ` `// Dividing k by their gcd ` ` ` `k /= g; ` ` ` ` ` `// Check for divisibility of X ` ` ` `// by reduced K ` ` ` `if` `(x % k == ` `0` `) ` ` ` `{ ` ` ` `System.out.print(` `"YES\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `System.out.print(` `"NO\n"` `); ` ` ` `} ` `} ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == ` `0` `? a : __gcd(b, a % b); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `x = ` `6` `, k = ` `9` `; ` ` ` `checkDivisible(x, k); ` `} ` `} ` ` ` `// This code is contributed by gauravrajput1 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation to ` `# check if the square of X is ` `# divisible by K ` ` ` `from` `math ` `import` `gcd ` ` ` `# Function to return if ` `# square of X is divisible ` `# by K ` `def` `checkDivisible(x, k): ` ` ` ` ` `# Finding gcd of x and k ` ` ` `g ` `=` `gcd(x, k) ` ` ` ` ` `# Dividing k by their gcd ` ` ` `k ` `/` `/` `=` `g ` ` ` ` ` `# Check for divisibility of X ` ` ` `# by reduced K ` ` ` `if` `(x ` `%` `k ` `=` `=` `0` `): ` ` ` `print` `(` `"YES"` `) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `x ` `=` `6` ` ` `k ` `=` `9` ` ` `checkDivisible(x, k); ` ` ` `# This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to check ` `// if the square of X is ` `// divisible by K ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return if ` `// square of X is divisible ` `// by K ` `static` `void` `checkDivisible(` `int` `x, ` `int` `k) ` `{ ` ` ` ` ` `// Finding gcd of x and k ` ` ` `int` `g = __gcd(x, k); ` ` ` ` ` `// Dividing k by their gcd ` ` ` `k /= g; ` ` ` ` ` `// Check for divisibility of X ` ` ` `// by reduced K ` ` ` `if` `(x % k == 0) ` ` ` `{ ` ` ` `Console.Write(` `"YES\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.Write(` `"NO\n"` `); ` ` ` `} ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == 0 ? a : __gcd(b, a % b); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `x = 6, k = 9; ` ` ` ` ` `checkDivisible(x, k); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

YES

## Recommended Posts:

- Largest Divisor of a Number not divisible by a perfect square
- Check if a number is perfect square without finding square root
- Check if a number is a perfect square having all its digits as a perfect square
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a large number is divisible by 11 or not
- Check if a large number is divisible by 3 or not
- Check if a large number is divisible by 8 or not
- Check if a large number is divisible by 6 or not
- Check if a large number is divisible by 9 or not
- Check if a large number is divisible by 5 or not
- Check a large number is divisible by 16 or not
- Check if a large number is divisible by 25 or not
- Check if LCM of array elements is divisible by a prime number or not
- Check if a large number is divisible by 13 or not
- Check if the large number formed is divisible by 41 or not
- Check if a number is divisible by 23 or not
- Check if any large number is divisible by 19 or not
- Check if any large number is divisible by 17 or not
- Check if a large number is divisible by 2, 3 and 5 or not

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.