Given a range **[L, R]**, the task is to print all the perfect squares from the given range.

**Examples:**

Input:L = 2, R = 24

Output:4 9 16

Input:L = 1, R = 100

Output:1 4 9 16 25 36 49 64 81 100

**Naive approach:** Starting from L to R check whether the current element is a perfect square or not. If yes then print it.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print all the perfect ` `// squares from the given range ` `void` `perfectSquares(` `float` `l, ` `float` `r) ` `{ ` ` ` ` ` `// For every element from the range ` ` ` `for` `(` `int` `i = l; i <= r; i++) { ` ` ` ` ` `// If current element is ` ` ` `// a perfect square ` ` ` `if` `(` `sqrt` `(i) == (` `int` `)` `sqrt` `(i)) ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 2, r = 24; ` ` ` ` ` `perfectSquares(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to print all the perfect ` `// squares from the given range ` `static` `void` `perfectSquares(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// For every element from the range ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `{ ` ` ` ` ` `// If current element is ` ` ` `// a perfect square ` ` ` `if` `(Math.sqrt(i) == (` `int` `)Math.sqrt(i)) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `l = ` `2` `, r = ` `24` `; ` ` ` `perfectSquares(l, r); ` `} ` `} ` ` ` `// This code is contributed by jit_t ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to print all the perfect ` `# squares from the given range ` `def` `perfectSquares(l, r): ` ` ` ` ` `# For every element from the range ` ` ` `for` `i ` `in` `range` `(l, r ` `+` `1` `): ` ` ` ` ` `# If current element is ` ` ` `# a perfect square ` ` ` `if` `(i` `*` `*` `(.` `5` `) ` `=` `=` `int` `(i` `*` `*` `(.` `5` `))): ` ` ` `print` `(i, end` `=` `" "` `) ` ` ` `# Driver code ` `l ` `=` `2` `r ` `=` `24` ` ` `perfectSquares(l, r) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to print all the perfect ` `// squares from the given range ` `static` `void` `perfectSquares(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// For every element from the range ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `{ ` ` ` ` ` `// If current element is ` ` ` `// a perfect square ` ` ` `if` `(Math.Sqrt(i) == (` `int` `)Math.Sqrt(i)) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `l = 2, r = 24; ` ` ` `perfectSquares(l, r); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

4 9 16

It is solution with O(n). moreover the use of number of square roots leads to computational expense.

**Efficient approach:** This method is based on the fact that the very first perfect square after number **L** will definitely be the square of **⌈sqrt(L)⌉**. In very simple terms, the square root of **L** will be very close to the number whose square root we are trying to find. Therefore, the number will be **pow(ceil(sqrt(L)), 2)**.

The very first perfect square is important for this method. Now the original answer is hidden over this pattern i.e. **0 1 4 9 16 25**

the difference between 0 and 1 is 1

the difference between 1 and 4 is 3

the difference between 4 and 9 is 5 and so on…

which means that the difference between two perfect squares is always an odd number.

Now, the question arises what must be added to get the next number and the answer is **(sqrt(X) * 2) + 1** where **X** is the already known perfect square.

Let the current perfect square be **4** then the next perfect square will definitely be **4 + (sqrt(4) * 2 + 1) = 9**. Here, number **5** is added and the next number to be added will be **7** then **9** and so on… which makes a series of odd numbers.

Addition is computationally less expensive than performing multiplication or finding square roots of every number.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print all the perfect ` `// squares from the given range ` `void` `perfectSquares(` `float` `l, ` `float` `r) ` `{ ` ` ` ` ` `// Getting the very first number ` ` ` `int` `number = ` `ceil` `(` `sqrt` `(l)); ` ` ` ` ` `// First number's square ` ` ` `int` `n2 = number * number; ` ` ` ` ` `// Next number is at the difference of ` ` ` `number = (number * 2) + 1; ` ` ` ` ` `// While the perfect squares ` ` ` `// are from the range ` ` ` `while` `((n2 >= l && n2 <= r)) { ` ` ` ` ` `// Print the perfect square ` ` ` `cout << n2 << ` `" "` `; ` ` ` ` ` `// Get the next perfect square ` ` ` `n2 = n2 + number; ` ` ` ` ` `// Next odd number to be added ` ` ` `number += 2; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 2, r = 24; ` ` ` ` ` `perfectSquares(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to print all the perfect ` `// squares from the given range ` `static` `void` `perfectSquares(` `float` `l, ` `float` `r) ` `{ ` ` ` ` ` `// Getting the very first number ` ` ` `int` `number = (` `int` `) Math.ceil(Math.sqrt(l)); ` ` ` ` ` `// First number's square ` ` ` `int` `n2 = number * number; ` ` ` ` ` `// Next number is at the difference of ` ` ` `number = (number * ` `2` `) + ` `1` `; ` ` ` ` ` `// While the perfect squares ` ` ` `// are from the range ` ` ` `while` `((n2 >= l && n2 <= r)) ` ` ` `{ ` ` ` ` ` `// Print the perfect square ` ` ` `System.out.print(n2 + ` `" "` `); ` ` ` ` ` `// Get the next perfect square ` ` ` `n2 = n2 + number; ` ` ` ` ` `// Next odd number to be added ` ` ` `number += ` `2` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `l = ` `2` `, r = ` `24` `; ` ` ` ` ` `perfectSquares(l, r); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `from` `math ` `import` `ceil, sqrt ` ` ` `# Function to print all the perfect ` `# squares from the given range ` `def` `perfectSquares(l, r) : ` ` ` ` ` ` ` `# Getting the very first number ` ` ` `number ` `=` `ceil(sqrt(l)); ` ` ` ` ` `# First number's square ` ` ` `n2 ` `=` `number ` `*` `number; ` ` ` ` ` `# Next number is at the difference of ` ` ` `number ` `=` `(number ` `*` `2` `) ` `+` `1` `; ` ` ` ` ` `# While the perfect squares ` ` ` `# are from the range ` ` ` `while` `((n2 >` `=` `l ` `and` `n2 <` `=` `r)) : ` ` ` ` ` `# Print the perfect square ` ` ` `print` `(n2, end` `=` `" "` `); ` ` ` ` ` `# Get the next perfect square ` ` ` `n2 ` `=` `n2 ` `+` `number; ` ` ` ` ` `# Next odd number to be added ` ` ` `number ` `+` `=` `2` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `l ` `=` `2` `; r ` `=` `24` `; ` ` ` ` ` `perfectSquares(l, r); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to print all the perfect ` `// squares from the given range ` `static` `void` `perfectSquares(` `float` `l, ` `float` `r) ` `{ ` ` ` ` ` `// Getting the very first number ` ` ` `int` `number = (` `int` `) Math.Ceiling(Math.Sqrt(l)); ` ` ` ` ` `// First number's square ` ` ` `int` `n2 = number * number; ` ` ` ` ` `// Next number is at the difference of ` ` ` `number = (number * 2) + 1; ` ` ` ` ` `// While the perfect squares ` ` ` `// are from the range ` ` ` `while` `((n2 >= l && n2 <= r)) ` ` ` `{ ` ` ` ` ` `// Print the perfect square ` ` ` `Console.Write(n2 + ` `" "` `); ` ` ` ` ` `// Get the next perfect square ` ` ` `n2 = n2 + number; ` ` ` ` ` `// Next odd number to be added ` ` ` `number += 2; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `l = 2, r = 24; ` ` ` ` ` `perfectSquares(l, r); ` `} ` `} ` ` ` `// This code is contributed by Rajput Ji ` |

*chevron_right*

*filter_none*

**Output:**

4 9 16

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.

## Recommended Posts:

- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Count of primes in a given range that can be expressed as sum of perfect squares
- Sum of distances between the two nearest perfect squares to all the nodes of the given linked list
- Largest sub-array whose all elements are perfect squares
- Largest number from the longest set of anagrams possible from all perfect squares of length K
- Number of perfect squares between two given numbers
- Count of perfect squares of given length
- Check if a number is a perfect square having all its digits as a perfect square
- Count number less than N which are product of perfect squares
- Sort perfect squares in an array at their relative positions
- Check if the sum of perfect squares in an array is divisible by x
- Smallest and Largest N-digit perfect squares
- Count elements in an Array that can be represented as difference of two perfect squares
- Subsets of size K with product equal to difference of two perfect squares
- Count numbers upto N which are both perfect square and perfect cube
- Find smallest perfect square number A such that N + A is also a perfect square number
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Sum of all Perfect numbers lying in the range [L, R]
- Probability of getting a perfect square when a random number is chosen in a given range
- Check if the sum of a subarray within a given range is a perfect square or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.