📜  给定长度的完美平方数

📅  最后修改于: 2021-06-26 18:45:00             🧑  作者: Mango

给定整数N ,任务是找到长度为N的完全平方的数量。
例子:

天真的方法:为了解决这个问题,我们可以检查10 (N – 1)10 N – 1之间的所有数字,并在遇到理想平方时递增计数器
下面是上述方法的实现:

C++
// C++ Program to count perfect
// squares of given length
 
#include 
using namespace std;
 
// Function to check if a
// number is perfect square
bool isPerfectSquare(long double x)
{
    // Find floating point value of
    // square root of x.
    long double sr = sqrt(x);
 
    // If square root is an integer
    return ((sr - floor(sr)) == 0);
}
 
// Function to return the count of
// n digit perfect squares
int countSquares(int n)
{
    // Initialize result
    int cnt = 0;
 
    // Traverse through all numbers
    // of n digits
    for (int i = pow(10, (n - 1));
         i < pow(10, n); i++) {
 
        // Check if current number
        // 'i' is perfect square
        if (i != 0 && isPerfectSquare(i))
            cnt++;
    }
    return cnt;
}
 
// Driver code
int main()
{
    int n = 3;
    cout << countSquares(n);
    return 0;
}


Java
// Java Program to count perfect
// squares of given length
class GFG{
 
// Function to check if a
// number is perfect square
static boolean isPerfectSquare(double x)
{
     
    // Find floating point value of
    // square root of x.
    double sr = Math.sqrt(x);
 
    // If square root is an integer
    return ((sr - Math.floor(sr)) == 0);
}
 
// Function to return the count of
// n digit perfect squares
static int countSquares(int n)
{
     
    // Initialize result
    int cnt = 0;
 
    // Traverse through all numbers
    // of n digits
    for(int i = (int) Math.pow(10, (n - 1));
                  i < Math.pow(10, n); i++)
    {
 
        // Check if current number
        // 'i' is perfect square
        if (i != 0 && isPerfectSquare(i))
            cnt++;
    }
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.print(countSquares(n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 Program to count perfect
# squares of given length
import math;
 
# Function to check if a
# number is perfect square
def isPerfectSquare(x):
 
    # Find floating point value of
    # square root of x.
    sr = math.sqrt(x);
 
    # If square root is an integer
    return ((sr - math.floor(sr)) == 0);
 
# Function to return the count of
# n digit perfect squares
def countSquares(n):
 
    # Initialize result
    cnt = 0;
 
    # Traverse through all numbers
    # of n digits
    for i in range(int(math.pow(10, (n - 1))),
                   int(math.pow(10, n))):
 
        # Check if current number
        # 'i' is perfect square
        if (i != 0 and isPerfectSquare(i)):
            cnt += 1;
     
    return cnt;
 
# Driver code
n = 3;
print(countSquares(n));
 
# This code is contributed by Akanksha_Rai


C#
// C# program to count perfect
// squares of given length
using System;
 
class GFG{
 
// Function to check if a
// number is perfect square
static bool isPerfectSquare(double x)
{
     
    // Find floating point value of
    // square root of x.
    double sr = Math.Sqrt(x);
 
    // If square root is an integer
    return ((sr - Math.Floor(sr)) == 0);
}
 
// Function to return the count of
// n digit perfect squares
static int countSquares(int n)
{
     
    // Initialize result
    int cnt = 0;
 
    // Traverse through all numbers
    // of n digits
    for(int i = (int) Math.Pow(10, (n - 1));
                  i < Math.Pow(10, n); i++)
    {
 
       // Check if current number
       // 'i' is perfect square
       if (i != 0 && isPerfectSquare(i))
           cnt++;
    }
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
     
    Console.Write(countSquares(n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


C++
// C++ Program to count
// perfect squares of given length
 
#include 
using namespace std;
 
// Function to return the count of
// n digit perfect squares
int countSquares(int n)
{
    int r = ceil(sqrt(pow(10, n)));
    int l = ceil(sqrt(pow(10, n - 1)));
    return r - l;
}
 
// Driver code
int main()
{
    int n = 3;
    cout << countSquares(n);
    return 0;
}


Java
// Java Program to count perfect
// squares of given length
class GFG{
 
// Function to return the count
// of n digit perfect squares
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;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.print(countSquares(n));
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program to count perfect 
# squares of given length
import math
 
# Function to return the count
# of n digit perfect squares
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;
     
# Driver code
n = 3;
print(countSquares(n));
 
# This code is contributed by grand_master


C#
// C# Program to count perfect
// squares of given length
using System;
class GFG{
 
// Function to return the count
// of n digit perfect squares
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;
}
 
// Driver code
public static void Main()
{
    int n = 3;
    Console.Write(countSquares(n));
}
}
 
// This code is contributed by Nidhi_Biet


Javascript


输出:
22

高效的方法:为了解决这个问题,我们可以使用以下公式简单地找到长度为N的完美平方数:

  1. 如果数字nd位,则{10}^{d-1}\le n< {10}^{d}
  2. 因此,一个完美的正方形N 2具有d位数如果{10}^{d-1}\le {n}^{2}< {10}^{d}       或者\sqrt{10}^{d-1}\le n<\sqrt{10}^d.
  3. 因此,计算N位完美平方所需的答案是\left\lceil\sqrt{10^N}\right\rceil-\left\lceil\sqrt{10^{N-1}}\right\rceil

下面是上述方法的实现:

C++

// C++ Program to count
// perfect squares of given length
 
#include 
using namespace std;
 
// Function to return the count of
// n digit perfect squares
int countSquares(int n)
{
    int r = ceil(sqrt(pow(10, n)));
    int l = ceil(sqrt(pow(10, n - 1)));
    return r - l;
}
 
// Driver code
int main()
{
    int n = 3;
    cout << countSquares(n);
    return 0;
}

Java

// Java Program to count perfect
// squares of given length
class GFG{
 
// Function to return the count
// of n digit perfect squares
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;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.print(countSquares(n));
}
}
 
// This code is contributed by Rohit_ranjan

Python3

# Python3 program to count perfect 
# squares of given length
import math
 
# Function to return the count
# of n digit perfect squares
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;
     
# Driver code
n = 3;
print(countSquares(n));
 
# This code is contributed by grand_master

C#

// C# Program to count perfect
// squares of given length
using System;
class GFG{
 
// Function to return the count
// of n digit perfect squares
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;
}
 
// Driver code
public static void Main()
{
    int n = 3;
    Console.Write(countSquares(n));
}
}
 
// This code is contributed by Nidhi_Biet

Java脚本


输出:
22

时间复杂度: O(N)

辅助空间: O(1)