Number of perfect squares between two given numbers
Last Updated :
01 Sep, 2022
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 square in given range is 4.
Input : a = 9, b = 25
Output : 3
The three perfect squares in given range are 9,
16 and 25
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int countSquares( int a, int b)
{
int cnt = 0;
for ( int i = a; i <= b; i++)
for ( int j = 1; j * j <= i; j++)
if (j * j == i)
cnt++;
return cnt;
}
int main()
{
int a = 9, b = 25;
cout << "Count of squares is "
<< countSquares(a, b);
return 0;
}
|
Java
class CountSquares {
static int countSquares( int a, int b)
{
int cnt = 0 ;
for ( int i = a; i <= b; i++)
for ( int j = 1 ; j * j <= i; j++)
if (j * j == i)
cnt++;
return cnt;
}
}
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));
}
}
|
Python3
def CountSquares(a, b):
cnt = 0
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
a = 9
b = 25
print ( "Count of squares is:" , CountSquares(a, b))
|
C#
using System;
class GFG {
static int countSquares( int a, int b)
{
int cnt = 0;
for ( int i = a; i <= b; i++)
for ( int j = 1; j * j <= i; j++)
if (j * j == i)
cnt++;
return cnt;
}
public static void Main()
{
int a = 9, b = 25;
Console.Write( "Count of squares is " + countSquares(a, b));
}
}
|
PHP
<?php
function countSquares( $a , $b )
{
$cnt = 0;
for ( $i = $a ; $i <= $b ; $i ++)
for ( $j = 1; $j * $j <= $i ;
$j ++)
if ( $j * $j == $i )
$cnt ++;
return $cnt ;
}
$a = 9; $b = 25;
echo "Count of squares is " .
countSquares( $a , $b );
?>
|
Javascript
<script>
function countSquares(a, b)
{
let cnt = 0;
for (let i = a; i <= b; i++)
for (let j = 1; j * j <= i;j++)
if (j * j == i)
cnt++;
return cnt;
}
let a = 9;
let b = 25;
document.write( "Count of squares is " ,
countSquares(a, b));
</script>
|
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)) + 1
We 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++
#include <bits/stdc++.h>
using namespace std;
int countSquares( int a, int b)
{
return ( floor ( sqrt (b)) - ceil ( sqrt (a)) + 1);
}
int main()
{
int a = 9, b = 25;
cout << "Count of squares is "
<< countSquares(a, b);
return 0;
}
|
Java
class CountSquares {
double countSquares( int a, int b)
{
return (Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1 );
}
}
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));
}
}
|
Python3
import math
def CountSquares(a, b):
return (math.floor(math.sqrt(b)) - math.ceil(math.sqrt(a)) + 1 )
a = 9
b = 25
print ( "Count of squares is:" , int (CountSquares(a, b)))
|
C#
using System;
class GFG {
static double countSquares( int a, int b)
{
return (Math.Floor(Math.Sqrt(b)) - Math.Ceiling(Math.Sqrt(a)) + 1);
}
public static void Main()
{
int a = 9, b = 25;
Console.Write( "Count of squares is " + ( int )countSquares(a, b));
}
}
|
PHP
<?php
function countSquares( $a , $b )
{
return ( floor (sqrt( $b )) -
ceil (sqrt( $a )) + 1);
}
{
$a = 9;
$b = 25;
echo "Count of squares is " ,
countSquares( $a , $b );
return 0;
}
?>
|
Javascript
<script>
function countSquares(a, b)
{
return (Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1);
}
let a = 9;
let b = 25;
document.write( "Count of squares is " ,
countSquares(a, b));
</script>
|
Output
Count of squares is 3
Time Complexity: O(logn)
Auxiliary Space: O(1)
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]
Share your thoughts in the comments
Please Login to comment...