Count of perfect squares of given length
Given an integer N, the task is to find the number of perfect squares of length N.
Examples:
Input: N = 1
Output: 3
Explanation: The single digit perfect squares are 1, 4 and 9.
Input: N = 2
Output: 6
Explanation: The two-digit perfect squares are 16, 25, 36, 49, 64 and 81.
Naive Approach: In order to solve this problem, we can check all the numbers between 10( N – 1 ) and 10N – 1 and increment the counter whenever we encounter a perfect square.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
int countSquares( int n)
{
int cnt = 0;
for ( int i = pow (10, (n - 1));
i < pow (10, n); i++) {
if (i != 0 && isPerfectSquare(i))
cnt++;
}
return cnt;
}
int main()
{
int n = 3;
cout << countSquares(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static int countSquares( int n)
{
int cnt = 0 ;
for ( int i = ( int ) Math.pow( 10 , (n - 1 ));
i < Math.pow( 10 , n); i++)
{
if (i != 0 && isPerfectSquare(i))
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.print(countSquares(n));
}
}
|
Python3
import math;
def isPerfectSquare(x):
sr = math.sqrt(x);
return ((sr - math.floor(sr)) = = 0 );
def countSquares(n):
cnt = 0 ;
for i in range ( int (math. pow ( 10 , (n - 1 ))),
int (math. pow ( 10 , n))):
if (i ! = 0 and isPerfectSquare(i)):
cnt + = 1 ;
return cnt;
n = 3 ;
print (countSquares(n));
|
C#
using System;
class GFG{
static bool isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static int countSquares( int n)
{
int cnt = 0;
for ( int i = ( int ) Math.Pow(10, (n - 1));
i < Math.Pow(10, n); i++)
{
if (i != 0 && isPerfectSquare(i))
cnt++;
}
return cnt;
}
public static void Main(String[] args)
{
int n = 3;
Console.Write(countSquares(n));
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
let sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function countSquares(n)
{
let cnt = 0;
for (let i = Math.pow(10, (n - 1));
i < Math.pow(10, n); i++) {
if (i != 0 && isPerfectSquare(i))
cnt++;
}
return cnt;
}
let n = 3;
document.write(countSquares(n));
</script>
|
Time Complexity: O(10n*log(n))
Auxiliary Space: O(1)
Efficient Approach: In order to solve this problem we can simply find the number of perfect squares of length N using a formula:
- A number n has d digits if
- Hence, a perfect square n2 has d digits if or
- Thus, the required answer for count of perfect squares of N digits is
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSquares( int n)
{
int r = ceil ( sqrt ( pow (10, n)));
int l = ceil ( sqrt ( pow (10, n - 1)));
return r - l;
}
int main()
{
int n = 3;
cout << countSquares(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int countSquares( int n)
{
int r = ( int ) Math.ceil(Math.sqrt
(Math.pow( 10 , n)));
int l = ( int ) Math.ceil(Math.sqrt
(Math.pow( 10 , n - 1 )));
return r - l;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.print(countSquares(n));
}
}
|
Python3
import math
def countSquares(n):
r = math.ceil(math.sqrt
(math. pow ( 10 , n)));
l = math.ceil(math.sqrt
(math. pow ( 10 , n - 1 )));
return r - l;
n = 3 ;
print (countSquares(n));
|
C#
using System;
class GFG{
static int countSquares( int n)
{
int r = ( int ) Math.Ceiling(Math.Sqrt
(Math.Pow(10, n)));
int l = ( int ) Math.Ceiling(Math.Sqrt
(Math.Pow(10, n - 1)));
return r - l;
}
public static void Main()
{
int n = 3;
Console.Write(countSquares(n));
}
}
|
Javascript
<script>
function countSquares(n)
{
let r = Math.ceil(Math.sqrt(Math.pow(10, n)));
let l = Math.ceil(Math.sqrt(Math.pow(10, n - 1)));
return r - l;
}
let n = 3;
document.write(countSquares(n));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...