![]() |
VOOZH | about |
Given a number, check if it is square-free or not. A number is said to be square-free if no prime factor divides it more than once, i.e., the largest power of a prime factor that divides n is one. First few square-free numbers are 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, ...
Examples:
Input: n = 10
Output: Yes
Explanation: 10 can be factorized as 2*5. Since no prime factor appears more than once, it is a square free number.Input: n = 20
Output: No
Explanation: 20 can be factorized as 2 * 2 * 5. Since prime factor appears more than once, it is not a square free number.
The idea is simple, one by one find all prime factors. For every prime factor, we check if its square also divides n. If yes, then we return false. Finally, if we do not find a prime factor that is divisible more than once, we return false.
Yes
Time Complexity: O(sqrt(N)), In the worst case when the number is a perfect square, then there will be sqrt(n)/2 iterations.
Auxiliary Space: O(1)