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 .
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.
- Nth Square free number
- Minimum number of Square Free Divisors
- Count of square free divisors of a given number
- Square free semiprimes in a given range using C++ STL
- Find smallest perfect square number A such that N + A is also a perfect square number
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is a perfect square having all its digits as a perfect square
- Check if a number is perfect square without finding square root
- Count square and non-square numbers before n
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Cube Free Numbers smaller than n
- 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
- Previous perfect square and cube number smaller than number N
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Square root of a number using log
- Demlo number (Square of 11...1)
- Check if given number is perfect square
- Find square root of number upto given precision using binary search
- Square pyramidal number (Sum of Squares)
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.