Given a number **X**, find the Smallest Number, which is greater than 1, that divides **X ^{X}**. In the given question, X is always presumed to be greater than 1.

**Examples:**

Input:X = 6

Output:2

Explanation:As, 6^{6}is equal to 46656, which is divisible by 2 and it’s the smallest among all its divisors.

Input:X = 3

Output:3

Explanation:As, 3^{3}is equal to 27, which is divisible by 3 and it’s the smallest among all its divisors.

**Approach:**

The main observation of this problem is that if a number **P** divides **X**, then it also divides **X ^{X}**, so we don’t need to calculate the value of

**X**. What we need to do is to find the smallest number that divides X which will always be a Prime Number.

^{X}Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the required smallest number ` `int` `SmallestDiv(` `int` `n) ` `{ ` ` ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) { ` ` ` `// Finding smallest number that divides n ` ` ` `if` `(n % i == 0) { ` ` ` ` ` `// i divides n and return this ` ` ` `// value immediately ` ` ` `return` `i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If n is a prime number then answer should be n, ` ` ` `// As we can't take 1 as our answer. ` ` ` `return` `n; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `X = 385; ` ` ` ` ` `int` `ans = SmallestDiv(X); ` ` ` `cout << ans << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG{ ` ` ` `// Function to find the ` `// required smallest number ` `static` `int` `SmallestDiv(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = ` `2` `; i * i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Finding smallest number ` ` ` `// that divides n ` ` ` `if` `(n % i == ` `0` `) ` ` ` `{ ` ` ` ` ` `// i divides n and return this ` ` ` `// value immediately ` ` ` `return` `i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If n is a prime number then ` ` ` `// answer should be n, as we ` ` ` `// can't take 1 as our answer. ` ` ` `return` `n; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `X = ` `385` `; ` ` ` `int` `ans = SmallestDiv(X); ` ` ` ` ` `System.out.print(ans + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by gauravrajput1 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function to find the required smallest number ` `def` `SmallestDiv(n): ` ` ` ` ` `i ` `=` `2` ` ` `while` `i ` `*` `i <` `=` `n: ` ` ` ` ` `# Finding smallest number that divides n ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` ` ` `# i divides n and return this ` ` ` `# value immediately ` ` ` `return` `i ` ` ` `i ` `+` `=` `1` ` ` ` ` `# If n is a prime number then answer should be n, ` ` ` `# As we can't take 1 as our answer. ` ` ` `return` `n ` ` ` `# Driver Code ` `if` `__name__` `=` `=` `"__main__"` `: ` ` ` ` ` `X ` `=` `385` ` ` `ans ` `=` `SmallestDiv(X) ` ` ` ` ` `print` `(ans) ` ` ` `# This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to find the ` `// required smallest number ` `static` `int` `SmallestDiv(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Finding smallest number ` ` ` `// that divides n ` ` ` `if` `(n % i == 0) ` ` ` `{ ` ` ` ` ` `// i divides n and return this ` ` ` `// value immediately ` ` ` `return` `i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If n is a prime number then ` ` ` `// answer should be n, as we ` ` ` `// can't take 1 as our answer. ` ` ` `return` `n; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `X = 385; ` ` ` `int` `ans = SmallestDiv(X); ` ` ` ` ` `Console.Write(ans + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

Output:5

**Time Complexity:** O(sqrt(X))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Smallest integer > 1 which divides every element of the given array
- Find the largest composite number that divides N but is strictly lesser than N
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Check if a M-th fibonacci number divides N-th fibonacci number
- Highest power of a number that divides other number
- Count of prime digits of a Number which divides the number
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Find the last digit when factorial of A divides factorial of B
- Find element in array that divides all array elements
- Find a distinct pair (x, y) in given range such that x divides y
- Greatest divisor which divides all natural number in range [L, R]
- Largest number that divides x and is co-prime with y
- Check if the sum of digits of a number N divides it
- Minimum value that divides one number and divisible by other
- Check if a given number divides the sum of the factorials of its digits
- Highest power of 2 that divides a number represented in binary

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.