Given a number X, find the Smallest Number, which is greater than 1, that divides XX. In the given question, X is always presumed to be greater than 1.
Examples:
Input: X = 6
Output: 2
Explanation: As, 66 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, 33 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 XX, so we don’t need to calculate the value of XX. What we need to do is to find the smallest number that divides X which will always be a Prime Number.
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 numberint 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 Codeint main(){ int X = 385; int ans = SmallestDiv(X); cout << ans << "\n"; return 0;} |
Java
// Java implementation of above approachclass GFG{// Function to find the// required smallest numberstatic 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 Codepublic static void main(String[] args){ int X = 385; int ans = SmallestDiv(X); System.out.print(ans + "\n");}}// This code is contributed by gauravrajput1 |
Python3
# Python3 implementation of above approach# Function to find the required smallest numberdef 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 Codeif __name__=="__main__": X = 385 ans = SmallestDiv(X) print(ans)# This code is contributed by Yash_R |
C#
// C# implementation of above approachusing System;class GFG { // Function to find the// required smallest numberstatic 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 codepublic static void Main(String[] args){ int X = 385; int ans = SmallestDiv(X); Console.Write(ans + "\n");}}// This code is contributed by shivanisinghss2110 |
Javascript
<script> // Javascript implementation of above approach // Function to find the required smallest number function SmallestDiv(n) { for (let 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; } let X = 385; let ans = SmallestDiv(X); document.write(ans + "</br>");</script> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live


