Many programmers love solving tricky mathematical problems using code. It helps sharpen the mind and improve problem-solving skills. In this article, you'll learn how to find the smallest and largest n-digit perfect squares and cubes using Python, C++, and JavaScript. Each example also contains sample output for several different values.

Smallest and Largest N-Digit Perfect Squares

Problem Statement

You're given an integer n, and you need to find the smallest and largest n-digit numbers that are also perfect squares.

Example 1: Let n = 2

Smallest 2-digit perfect square is 16 and the largest 2-digit perfect square is 81.

Thus, the output is:

Smallest 2-digit perfect square: 16

Largest 2-digit perfect square: 81

Example 2: Let n = 3

Smallest 3-digit perfect square is 100 and the largest 3-digit perfect square is 961.

Thus, the output is:

Smallest 3-digit perfect square: 100

Largest 3-digit perfect square: 961

Approach to Solve the Problem

You can find the smallest n-digit perfect square using the following formula:

        pow(ceil(sqrt(pow(10, n – 1))), 2)
    

And to find the largest n-digit perfect square, use the following formula:

        pow(ceil(sqrt(pow(10, n))) – 1, 2)
    

C++ Program to Find the Smallest and Largest N-Digit Perfect Squares

Below is the C++ program to find the smallest and largest n-digit perfect squares:

        // C++ program to find the smallest and largest
// n-digit perfect squares
#include <bits/stdc++.h>
using namespace std;

void findPerfectSquares(int n)
{
    cout << "Smallest "<< n << "-digit perfect square: " << pow(ceil(sqrt(pow(10, n - 1))), 2) << endl;

    cout << "Largest " << n << "-digit perfect square: " << pow(ceil(sqrt(pow(10, n))) - 1, 2) << endl;
}


int main()
{
    int n1 = 1;
    cout << "Number of digits: " << n1 << endl;
    findPerfectSquares(n1);

    int n2 = 2;
    cout << "Number of digits: " << n2 << endl;
    findPerfectSquares(n2);

    int n3 = 3;
    cout << "Number of digits: " << n3 << endl;
    findPerfectSquares(n3);

    int n4 = 4;
    cout << "Number of digits: " << n4 << endl;
    findPerfectSquares(n4);

    return 0;
}

Output:

        Number of digits: 1
Smallest 1-digit perfect square: 1
Largest 1-digit perfect square: 9
Number of digits: 2
Smallest 2-digit perfect square: 16
Largest 2-digit perfect square: 81
Number of digits: 3
Smallest 3-digit perfect square: 100
Largest 3-digit perfect square: 961
Number of digits: 4
Smallest 4-digit perfect square: 1024
Largest 4-digit perfect square: 9801

Related: How to Calculate the Value of nCr

Python Program to Find the Smallest and Largest N-Digit Perfect Squares

Below is the Python program to find the smallest and largest n-digit perfect squares:

        # Python program to find the smallest and largest
# n-digit perfect squares
import math

def findPerfectSquares(n):
    print("Smallest ", n,"-digit perfect square:", pow(math.ceil(math.sqrt(pow(10, n - 1))), 2))

    print("Largest ", n,"-digit perfect square:", pow(math.ceil(math.sqrt(pow(10, n))) - 1, 2))


n1 = 1
print("Number of digits:", n1)
findPerfectSquares(n1)

n2 = 2
print("Number of digits:", n2)
findPerfectSquares(n2)

n3 = 3
print("Number of digits:", n3)
findPerfectSquares(n3)

n4 = 4
print("Number of digits:", n4)
findPerfectSquares(n4)

Output:

        Number of digits: 1
Smallest 1 -digit perfect square: 1
Largest 1 -digit perfect square: 9
Number of digits: 2
Smallest 2 -digit perfect square: 16
Largest 2 -digit perfect square: 81
Number of digits: 3
Smallest 3 -digit perfect square: 100
Largest 3 -digit perfect square: 961
Number of digits: 4
Smallest 4 -digit perfect square: 1024
Largest 4 -digit perfect square: 9801

Related: How to Find the Largest and Smallest Digits of a Number With Programming

JavaScript Program to Find the Smallest and Largest N-Digit Perfect Squares

Below is the JavaScript program to find the smallest and largest n-digit perfect squares:

        // JavaScript program to find the smallest and largest
// n-digit perfect squares

function findPerfectSquares(n) {
    document.write("Smallest " + n + "-digit perfect square: " + Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n - 1))), 2) + "
");

    document.write("Largest " + n + "-digit perfect square: " + Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n))) - 1, 2) + "
");
}



var n1 = 1;
document.write("Number of digits: " + n1 + "
");
findPerfectSquares(n1);

var n2 = 2;
document.write("Number of digits: " + n2 + "
");
findPerfectSquares(n2);

var n3 = 3;
document.write("Number of digits: " + n3 + "
");
findPerfectSquares(n3);

var n4 = 4;
document.write("Number of digits: " + n4 + "
");
findPerfectSquares(n4);

Output:

        Number of digits: 1
Smallest 1-digit perfect square: 1
Largest 1-digit perfect square: 9
Number of digits: 2
Smallest 2-digit perfect square: 16
Largest 2-digit perfect square: 81
Number of digits: 3
Smallest 3-digit perfect square: 100
Largest 3-digit perfect square: 961
Number of digits: 4
Smallest 4-digit perfect square: 1024
Largest 4-digit perfect square: 9801

Smallest and Largest N-Digit Perfect Cubes

Problem Statement

You're given an integer n, you need to find the smallest and largest n-digit numbers that are also perfect cubes.

Example 1: Let n = 2

Smallest 2-digit perfect cube is 27 and the largest 2-digit perfect cube is 64.

Thus, the output is:

Smallest 2-digit perfect cube: 27

Largest 2-digit perfect cube: 64

Example 2: Let n = 3

Smallest 3-digit perfect cube is 120 and the largest 3-digit perfect cube is 729.

Thus, the output is:

Smallest 3-digit perfect cube: 125

Largest 3-digit perfect cube: 729

Approach to Solve the Problem

You can find the smallest n-digit perfect cube using the following formula:

        pow(ceil(cbrt(pow(10, (n &ndash; 1)))), 3)
    

And to find the largest n-digit perfect cube, use the following formula:

        pow(ceil(cbrt(pow(10, (n))))-1, 3)
    

C++ Program to Find the Smallest and Largest N-Digit Perfect Cubes

Below is the C++ program to find the smallest and largest n-digit perfect cubes:

        // C++ program to find the smallest and largest
// n-digit perfect cubes
#include <bits/stdc++.h>
using namespace std;

void findPerfectCubes(int n)
{
    cout << "Smallest "<< n << "-digit perfect cube: " << pow(ceil(cbrt(pow(10, (n - 1)))), 3) << endl;

    cout << "Largest " << n << "-digit perfect cube: " << (int)pow(ceil(cbrt(pow(10, (n)))) - 1, 3) << endl;
}


int main()
{
    int n1 = 1;
    cout << "Number of digits: " << n1 << endl;
    findPerfectCubes(n1);

    int n2 = 2;
    cout << "Number of digits: " << n2 << endl;
    findPerfectCubes(n2);

    int n3 = 3;
    cout << "Number of digits: " << n3 << endl;
    findPerfectCubes(n3);

    int n4 = 4;
    cout << "Number of digits: " << n4 << endl;
    findPerfectCubes(n4);

    return 0;
}

Output:

        Number of digits: 1
Smallest 1-digit perfect cube: 1
Largest 1-digit perfect cube: 8
Number of digits: 2
Smallest 2-digit perfect cube: 27
Largest 2-digit perfect cube: 64
Number of digits: 3
Smallest 3-digit perfect cube: 125
Largest 3-digit perfect cube: 729
Number of digits: 4
Smallest 4-digit perfect cube: 1000
Largest 4-digit perfect cube: 9261

Python Program to Find the Smallest and Largest N-Digit Perfect Cubes

Below is the Python program to find the smallest and largest n-digit perfect cubes:

        # Python program to find the smallest and largest
# n-digit perfect cubes
import math

def findPerfectCubes(n):
    print("Smallest ", n,"-digit perfect cube:", pow(math.ceil((pow(10, (n - 1))) ** (1 / 3)), 3) )

    print("Largest ", n,"-digit perfect cube:", pow(math.ceil((pow(10, (n))) ** (1 / 3)) - 1, 3))


n1 = 1
print("Number of digits:", n1)
findPerfectCubes(n1)

n2 = 2
print("Number of digits:", n2)
findPerfectCubes(n2)

n3 = 3
print("Number of digits:", n3)
findPerfectCubes(n3)

n4 = 4
print("Number of digits:", n4)
findPerfectCubes(n4)

Output:

        Number of digits: 1
Smallest 1 -digit perfect cube: 1
Largest 1 -digit perfect cube: 8
Number of digits: 2
Smallest 2 -digit perfect cube: 27
Largest 2 -digit perfect cube: 64
Number of digits: 3
Smallest 3 -digit perfect cube: 125
Largest 3 -digit perfect cube: 729
Number of digits: 4
Smallest 4 -digit perfect cube: 1000
Largest 4 -digit perfect cube: 9261

JavaScript Program to Find the Smallest and Largest N-Digit Perfect Cubes

Below is the JavaScript program to find the smallest and largest n-digit perfect cubes:

        // JavaScript program to find the smallest and largest
// n-digit perfect cubes

function findPerfectCubes(n) {
    document.write("Smallest " + n + "-digit perfect cube: " + Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n - 1)))), 3) + "
");

    document.write("Largest " + n + "-digit perfect cube: " + Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n)))) - 1, 3) + "
");
}



var n1 = 1;
document.write("Number of digits: " + n1 + "
");
findPerfectCubes(n1);

var n2 = 2;
document.write("Number of digits: " + n2 + "
");
findPerfectCubes(n2);

var n3 = 3;
document.write("Number of digits: " + n3 + "
");
findPerfectCubes(n3);

var n4 = 4;
document.write("Number of digits: " + n4 + "
");
findPerfectCubes(n4);

Output:

        Number of digits: 1
Smallest 1-digit perfect cube: 1
Largest 1-digit perfect cube: 8
Number of digits: 2
Smallest 2-digit perfect cube: 27
Largest 2-digit perfect cube: 64
Number of digits: 3
Smallest 3-digit perfect cube: 125
Largest 3-digit perfect cube: 729
Number of digits: 4
Smallest 4-digit perfect cube: 1000
Largest 4-digit perfect cube: 9261

Sharpen Your Brain With Stimulating Math Puzzles

If you're someone who loves solving math puzzles and riddles, you're doing your brain a favor! Solving math puzzles and riddles improves memory, increases problem-solving skills, and can also increase IQ. Some great websites, YouTube channels, and apps provide amazing math puzzles and games for free.