# Number of perfect squares between two given numbers

Given two given numbers a and b where 1<=a<=b, find the number of perfect squares between a and b (a and b inclusive).

Examples

Input : a = 3, b = 8 Output : 1 The only perfect in given range is 4. Input : a = 9, b = 25 Output : 3 The three squares in given range are 9, 16 and 25

**Method 1** : One naive approach is to check all the numbers between a and b (inclusive a and b) and increase count by one whenever we encounter a perfect square.

Below is the implementation of above idea :

## C++

`// A Simple Method to count squares between a and b ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countSquares(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `cnt = 0; ` `// Initialize result ` ` ` ` ` `// Traverse through all numbers ` ` ` `for` `(` `int` `i = a; i <= b; i++) ` ` ` ` ` `// Check if current number 'i' is perfect ` ` ` `// square ` ` ` `for` `(` `int` `j = 1; j * j <= i; j++) ` ` ` `if` `(j * j == i) ` ` ` `cnt++; ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 9, b = 25; ` ` ` `cout << ` `"Count of squares is "` ` ` `<< countSquares(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count squares between a and b ` `class` `CountSquares { ` ` ` ` ` `static` `int` `countSquares(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `int` `cnt = ` `0` `; ` `// Initialize result ` ` ` ` ` `// Traverse through all numbers ` ` ` `for` `(` `int` `i = a; i <= b; i++) ` ` ` ` ` `// Check if current number 'i' is perfect ` ` ` `// square ` ` ` `for` `(` `int` `j = ` `1` `; j * j <= i; j++) ` ` ` `if` `(j * j == i) ` ` ` `cnt++; ` ` ` `return` `cnt; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `class` `PerfectSquares { ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `9` `, b = ` `25` `; ` ` ` `CountSquares obj = ` `new` `CountSquares(); ` ` ` `System.out.print(` `"Count of squares is "` `+ obj.countSquares(a, b)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program to count squares between a and b ` ` ` `def` `CountSquares(a, b): ` ` ` ` ` `cnt ` `=` `0` `# initialize result ` ` ` ` ` `# Traverse through all numbers ` ` ` `for` `i ` `in` `range` `(a, b ` `+` `1` `): ` ` ` `j ` `=` `1` `; ` ` ` `while` `j ` `*` `j <` `=` `i: ` ` ` `if` `j ` `*` `j ` `=` `=` `i: ` ` ` `cnt ` `=` `cnt ` `+` `1` ` ` `j ` `=` `j ` `+` `1` ` ` `i ` `=` `i ` `+` `1` ` ` `return` `cnt ` ` ` `# Driver Code ` `a ` `=` `9` `b ` `=` `25` `print` `"Count of squares is:"` `, CountSquares(a, b) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count squares ` `// between a and b ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count squares ` ` ` `static` `int` `countSquares(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Traverse through all numbers ` ` ` `for` `(` `int` `i = a; i <= b; i++) ` ` ` ` ` `// Check if current number ` ` ` `// 'i' is perfect square ` ` ` `for` `(` `int` `j = 1; j * j <= i; j++) ` ` ` `if` `(j * j == i) ` ` ` `cnt++; ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 9, b = 25; ` ` ` `Console.Write(` `"Count of squares is "` `+ countSquares(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// A Simple Method to count squares ` `//between a and b ` ` ` `function` `countSquares(` `$a` `, ` `$b` `) ` `{ ` ` ` `$cnt` `= 0; ` `// Initialize result ` ` ` ` ` `// Traverse through all numbers ` ` ` `for` `(` `$i` `= ` `$a` `; ` `$i` `<= ` `$b` `; ` `$i` `++) ` ` ` ` ` `// Check if current number ` ` ` `// 'i' is perfect square ` ` ` `for` `(` `$j` `= 1; ` `$j` `* ` `$j` `<= ` `$i` `; ` ` ` `$j` `++) ` ` ` `if` `(` `$j` `* ` `$j` `== ` `$i` `) ` ` ` `$cnt` `++; ` ` ` ` ` `return` `$cnt` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `$a` `= 9; ` `$b` `= 25; ` ` ` `echo` `"Count of squares is "` `. ` ` ` `countSquares(` `$a` `, ` `$b` `); ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output :

Count of squares is 3

An upper bound on time Complexity of this solution is O((b-a) * sqrt(b)).

**Method 2 (Efficient)** We can simply take square root of ‘a’ and square root of ‘b’ and count the perfect squares between them using

floor(sqrt(b)) - ceil(sqrt(a)) + 1We take floor of sqrt(b) because we need to consider numbers before b. We take ceil of sqrt(a) because we need to consider numbers after a. For example, let b = 24, a = 8. floor(sqrt(b)) = 4, ceil(sqrt(a)) = 3. And number of squares is 4 - 3 + 1 = 2. The two numbers are 9 and 16.

Below is the implementation of above idea :

## C++

`// An Efficient Method to count squares between a and b ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// An efficient solution to count square between a ` `// and b ` `int` `countSquares(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(` `floor` `(` `sqrt` `(b)) - ` `ceil` `(` `sqrt` `(a)) + 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 9, b = 25; ` ` ` `cout << ` `"Count of squares is "` ` ` `<< countSquares(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// An Efficient method to count squares between ` `// a and b ` `class` `CountSquares { ` ` ` `double` `countSquares(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + ` `1` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `class` `PerfectSquares { ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `9` `, b = ` `25` `; ` ` ` `CountSquares obj = ` `new` `CountSquares(); ` ` ` `System.out.print(` `"Count of squares is "` `+ (` `int` `)obj.countSquares(a, b)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# An Efficient Method to count squares between a ` `# and b ` `import` `math ` `def` `CountSquares(a, b): ` ` ` `return` `(math.floor(math.sqrt(b)) ` `-` `math.ceil(math.sqrt(a)) ` `+` `1` `) ` ` ` `# Driver Code ` `a ` `=` `9` `b ` `=` `25` `print` `"Count of squares is:"` `, ` `int` `(CountSquares(a, b)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program for efficient method ` `// to count squares between a & b ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count squares ` ` ` `static` `double` `countSquares(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(Math.Floor(Math.Sqrt(b)) - Math.Ceiling(Math.Sqrt(a)) + 1); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 9, b = 25; ` ` ` `Console.Write(` `"Count of squares is "` `+ (` `int` `)countSquares(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// An Efficient PHP code to count ` `// squares between a and b ` ` ` `// Method to count square ` `// between a and b ` `function` `countSquares(` `$a` `, ` `$b` `) ` `{ ` ` ` `return` `(` `floor` `(sqrt(` `$b` `)) - ` ` ` `ceil` `(sqrt(` `$a` `)) + 1); ` `} ` ` ` `// Driver code ` `{ ` ` ` `$a` `= 9; ` ` ` `$b` `= 25; ` ` ` `echo` `"Count of squares is "` `, ` ` ` `countSquares(` `$a` `, ` `$b` `); ` ` ` `return` `0; ` `} ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output :

Count of squares is 3

Time complexity of this solution is O(Log b). A typical implementation of square root for a number n takes time equal to O(Log n) [See this for a sample implementation of square root]

This article is contributed by **Rahul Aggarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. 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

## Recommended Posts:

- Count number less than N which are product of perfect squares
- Number of perfect cubes between two given numbers
- Print all perfect squares from the given range
- Count numbers upto N which are both perfect square and perfect cube
- Largest sub-array whose all elements are perfect squares
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Check if the sum of perfect squares in an array is divisible by x
- Smallest and Largest N-digit perfect squares
- Sort perfect squares in an array at their relative positions
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Sum of squares of first n natural numbers
- Sum of squares of first n natural numbers
- Sum of squares of Fibonacci numbers
- Average of Squares of Natural Numbers