Given a number n, we need to count square numbers smaller than or equal to n.
Input : n = 5 Output : Square Number : 2 Non-square numbers : 3 Explanation : Square numbers are 1 and 4. Non square numbers are 2, 3 and 5. Input : n = 10 Output : Square Number : 3 Non-square numbers : 7 Explanation : Square numbers are 1, 4 and 9. Non square numbers are 2, 3, 5, 6, 7, 8 and 10.
A simple solution is to traverse through all numbers from 1 to n and for every number check if n is perfect square or not.
An efficient solution is based on below formula.
Count of square numbers that are greater than 0 and smaller than or equal to n are floor(sqrt(n)) or ⌊√(n)⌋
Count of non-square numbers = n – ⌊√(n)⌋
Count of squares 3 Count of non-squares 7
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of subarrays having exactly K perfect square numbers
- Count numbers upto N which are both perfect square and perfect cube
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Sum of square of first n odd numbers
- Sum of square of first n even numbers
- Program to print non square numbers
- Print n numbers such that their sum is a perfect square
- Sum of square-sums of first n natural numbers
- Sum of all perfect square divisors of numbers from 1 to N
- Count of subarrays whose sum is a perfect square
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Count of pairs in an array whose sum is a perfect square
- Count quadruplets(A, B, C, D) till N such that sum of square of A and B is equal to that of C and D
- Count of square free divisors of a given number
- Count of elements which is product of a pair or an element square
- Count of subsequences whose product is a difference of square of two integers
- Count of pairs in an array whose product is a perfect square
- Count of integral coordinates that lies inside a Square
- Count of ways to represent N as sum of a prime number and twice of a square
- Check if product of array containing prime numbers is a perfect square
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.