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++
#include <bits/stdc++.h>
using namespace std;
void perfectSquares( float l, float r)
{
for ( int i = l; i <= r; i++) {
if ( sqrt (i) == ( int ) sqrt (i))
cout << i << " " ;
}
}
int main()
{
int l = 2, r = 24;
perfectSquares(l, r);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void perfectSquares( int l, int r)
{
for ( int i = l; i <= r; i++)
{
if (Math.sqrt(i) == ( int )Math.sqrt(i))
System.out.print(i + " " );
}
}
public static void main (String[] args)
{
int l = 2 , r = 24 ;
perfectSquares(l, r);
}
}
|
Python3
def perfectSquares(l, r):
for i in range (l, r + 1 ):
if (i * * (. 5 ) = = int (i * * (. 5 ))):
print (i, end = " " )
l = 2
r = 24
perfectSquares(l, r)
|
C#
using System;
class GFG
{
static void perfectSquares( int l, int r)
{
for ( int i = l; i <= r; i++)
{
if (Math.Sqrt(i) == ( int )Math.Sqrt(i))
Console.Write(i + " " );
}
}
public static void Main(String[] args)
{
int l = 2, r = 24;
perfectSquares(l, r);
}
}
|
Javascript
<script>
function perfectSquares(l, r){
for (let i = l; i <= r; i++)
{
if (Math.sqrt(i) == parseInt(Math.sqrt(i)))
document.write(i + " " );
}
}
let l = 2;
let r = 24;
perfectSquares(l, r)
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
void perfectSquares( float l, float r)
{
int number = ceil ( sqrt (l));
int n2 = number * number;
number = (number * 2) + 1;
while ((n2 >= l && n2 <= r)) {
cout << n2 << " " ;
n2 = n2 + number;
number += 2;
}
}
int main()
{
int l = 2, r = 24;
perfectSquares(l, r);
return 0;
}
|
Java
class GFG
{
static void perfectSquares( float l, float r)
{
int number = ( int ) Math.ceil(Math.sqrt(l));
int n2 = number * number;
number = (number * 2 ) + 1 ;
while ((n2 >= l && n2 <= r))
{
System.out.print(n2 + " " );
n2 = n2 + number;
number += 2 ;
}
}
public static void main(String[] args)
{
int l = 2 , r = 24 ;
perfectSquares(l, r);
}
}
|
Python3
from math import ceil, sqrt
def perfectSquares(l, r) :
number = ceil(sqrt(l));
n2 = number * number;
number = (number * 2 ) + 1 ;
while ((n2 > = l and n2 < = r)) :
print (n2, end = " " );
n2 = n2 + number;
number + = 2 ;
if __name__ = = "__main__" :
l = 2 ; r = 24 ;
perfectSquares(l, r);
|
C#
using System;
class GFG
{
static void perfectSquares( float l, float r)
{
int number = ( int ) Math.Ceiling(Math.Sqrt(l));
int n2 = number * number;
number = (number * 2) + 1;
while ((n2 >= l && n2 <= r))
{
Console.Write(n2 + " " );
n2 = n2 + number;
number += 2;
}
}
public static void Main(String[] args)
{
int l = 2, r = 24;
perfectSquares(l, r);
}
}
|
Javascript
<script>
function perfectSquares(l, r)
{
let number = Math.ceil(Math.sqrt(l));
let n2 = number * number;
number = (number * 2) + 1;
while ((n2 >= l && n2 <= r)) {
document.write(n2 + " " );
n2 = n2 + number;
number += 2;
}
}
let l = 2, r = 24;
perfectSquares(l, r);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Sep, 2022
Like Article
Save Article