Skip to content
Related Articles

Related Articles

Improve Article

Smallest and Largest N-digit perfect squares

  • Difficulty Level : Medium
  • Last Updated : 26 Mar, 2021
Geek Week

Given an integer N, the task is to find the smallest and the largest N digit numbers which are also perfect squares.
Examples: 
 

Input: N = 2 
Output: 16 81 
16 and 18 are the smallest and the largest 2-digit perfect squares.
Input: N = 3 
Output: 100 961 
 

 

Approach: For increasing values of N starting from N = 1, the series will go on like 9, 81, 961, 9801, ….. for the largest N-digit perfect square whose Nth term will be pow(ceil(sqrt(pow(10, N))) – 1, 2)
And 1, 16, 100, 1024, ….. for the smallest N-digit perfect square whose Nth term will be pow(ceil(sqrt(pow(10, N – 1))), 2).
Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
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




<?php
// PHP implementation of the approach
 
// Function to print the largest and
// the smallest n-digit perfect squares
function nDigitPerfectSquares($n)
{
 
    // Smallest n-digit perfect square
    echo pow(ceil(sqrt(pow(10, $n - 1))), 2), " ";
 
    // Largest n-digit perfect square
    echo pow(ceil(sqrt(pow(10, $n))) - 1, 2);
}
 
// Driver code
$n = 4;
nDigitPerfectSquares($n);
 
// This code is contributed by jit_t
?>

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to print the largest and
// the smallest n-digit perfect squares
function nDigitPerfectSquares(n)
{
 
    // Smallest n-digit perfect square
    document.write(Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n - 1))), 2) + " ");
 
    // Largest n-digit perfect square
    document.write(Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n))) - 1, 2));
}
 
// Driver code
var n = 4;
nDigitPerfectSquares(n);
 
// This code is contributed by rutvik_56.
</script>
Output: 
1024 9801

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :