# Print all perfect squares from the given range

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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; ` `} `

## 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 `

## 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 `

## 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 `

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 ` `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; ` `} `

## 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 `

## 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 `

## 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 `

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.