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., 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, …
Input : n = 10 Output : Yes 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 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, we 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 which is divisible more than once, we return false.
Time Complexity : O(sqrt(N))
In the worst case when the number is a perfect square, then there will be sqrt(n)/2 iterations .
- Nth Square free number
- Minimum number of Square Free Divisors
- Square free semiprimes in a given range using C++ STL
- Check if a number is perfect square without finding square root
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Nth non-Square number
- Centered Square Number
- Demlo number (Square of 11...1)
- N-th number which is both a square and a cube
- Square root of a number using log
- Largest number that is not a perfect square
- Check if given number is perfect square
- Square pyramidal number (Sum of Squares)
- Square root of a number without using sqrt() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.