Given a number n, find the n-th square-free number. A number is square-free if it is not divisible by a perfect square other than 1.
Input : n = 2 Output : 2 Input : 5 Output : 6 There is one number (in range from 1 to 6) that is divisible by a square. The number is 4.
Method 1 (Brute Force):
The idea is to iteratively check every number whether it is divisible by any perfect square number and increase the count whenever an square free number is encountered and returning the nth square free number.
Following is the implementation:
Method 2 (Better approach):
Idea is to count square free numbers less than or equal to upper limit ‘k’ and then apply binary search to find n-th square free number. First, we calculate count of square numbers (numbers with squares as factors) upto ‘k’ and then subtracting that count from total numbers to get count of square free numbers upto ‘k’.
- If any integer has a perfect square as factor, then it is guaranteed that it has a square of prime as a factor too. So we need to count the integers less than or equal to ‘k’ which have square of primes as a factor.
For example, find number of integers who have either 4 or 9 as a factor upto ‘k’. It can done using Inclusion–exclusion principle. Using Inclusion–exclusion principle, total number of integers are [k/4] + [k/9] -[k/36] where  is greatest integer function.
- Recursively apply inclusion and exclusion till the value of greatest integer becomes zero. This step will return count of numbers with squares as factors.
- Apply binary search to find the nth square free number.
Following is the implementation of above algorithm:
This article is contributed by Rahul Agrawal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Minimum number of Square Free Divisors
- Square Free Number
- 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
- Nth non-Square number
- Check if a number is perfect square without finding square root
- 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
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Count square and non-square numbers before n
- Cube Free Numbers smaller than n
- Number of digits in the nth number made of given four digits
- Find Nth number in a sequence which is not a multiple of a given number
- 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
- Find nth Magic Number
- Nth Even Fibonacci Number
- Program to find last two digits of Nth Fibonacci number