📌  相关文章
📜  最小和最大N位完美平方

📅  最后修改于: 2021-05-05 00:29:32             🧑  作者: Mango

给定一个整数N,任务是找到最小和最大的N个数字,它们也是理想的平方。
例子:

方法:为了提高从N = 1开始的N个值,该系列将继续像9,81,961,9801,… ..(最大N位完美的正方形,其第n项POW(小区(POW开方(10,N)))– 1,2)
以及1,16,100,1024,…..对于最小的N位完美平方,第N个项将是pow(ceil(sqrt(pow(10,N – 1))),2)
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the largest and
// the smallest n-digit perfect squares
void nDigitPerfectSquares(int n)
{
 
    // Smallest n-digit perfect square
    cout << pow(ceil(sqrt(pow(10, n - 1))), 2) << " ";
 
    // Largest n-digit perfect square
    cout << pow(ceil(sqrt(pow(10, n))) - 1, 2);
}
 
// Driver code
int main()
{
    int n = 4;
    nDigitPerfectSquares(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to print the largest and
    // the smallest n-digit perfect squares
    static void nDigitPerfectSquares(int n)
    {
        // Smallest n-digit perfect square
        int smallest = (int)Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n - 1))), 2);
        System.out.print(smallest + " ");
 
        // Largest n-digit perfect square
        int largest = (int)Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n))) - 1, 2);
        System.out.print(largest);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
        nDigitPerfectSquares(n);
    }
}


Python3
# Python3 implementation of the approach
import math
 
# Function to print the largest and
# the smallest n-digit perfect squares
def nDigitPerfectSquares(n):
 
    # Smallest n-digit perfect square
    print(pow(math.ceil(math.sqrt(pow(10, n - 1))), 2),
                                            end = " ");
 
    # Largest n-digit perfect square
    print(pow(math.ceil(math.sqrt(pow(10, n))) - 1, 2));
 
# Driver code
n = 4;
nDigitPerfectSquares(n);
 
# This code is contributed by mits


C#
// C# implementation of the approach
using System;
public class GFG {
  
    // Function to print the largest and
    // the smallest n-digit perfect squares
    static void nDigitPerfectSquares(int n)
    {
        // Smallest n-digit perfect square
        int smallest = (int)Math.Pow(Math.Ceiling(Math.Sqrt(Math.Pow(10, n - 1))), 2);
        Console.Write(smallest + " ");
  
        // Largest n-digit perfect square
        int largest = (int)Math.Pow(Math.Ceiling(Math.Sqrt(Math.Pow(10, n))) - 1, 2);
        Console.Write(largest);
    }
  
    // Driver code
    public static void Main(String []args)
    {
        int n = 4;
        nDigitPerfectSquares(n);
    }
}
 
// This code has been contributed by 29AjayKumar


PHP


Javascript


输出:
1024 9801