Related Articles
Print all perfect squares from the given range
• Last Updated : 08 Apr, 2021

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

## Javascript

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

## Javascript

 ``
Output:
`4 9 16`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up