Many programmers enjoy solving tricky math problems using code. It helps sharpen the mind and improve problem solving skills. In this article, you will 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

### Statement of the problem

We give you an integer m, and you need to find the smallest and largest n-digit numbers that are also perfect squares.

Example 1: Let n = 2

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

So the output is:

Smallest perfect square with 2 digits: 16

Largest perfect 2-digit square: 81

Example 2: Let n = 3

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

So the output is:

Smallest perfect square with 3 digits: 100

Largest perfect 3-digit square: 961

### Approach to solving the problem

You can find the smallest perfect square with n digits using the following formula:

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

And to find the greatest perfect square with n digits, use the following formula:

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

### C ++ program for finding 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 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;}``

Go out:

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

Related: How to Calculate the Value of nCr

### Python program for finding 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 squaresimport mathdef 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 = 1print("Number of digits:", n1)findPerfectSquares(n1)n2 = 2print("Number of digits:", n2)findPerfectSquares(n2)n3 = 3print("Number of digits:", n3)findPerfectSquares(n3)n4 = 4print("Number of digits:", n4)findPerfectSquares(n4)``

Go out:

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

Related: How To Find The Biggest And Smallest Digits In A Number With Programming

### JavaScript program to find the smallest and largest perfect squares with N-digits

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 squaresfunction 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);``

Go out:

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

Smallest and Largest N-Digit Perfect Cubes

### Statement of the problem

We give you an integer m, you need to find the smallest and largest n-digit numbers which are also perfect cubes.

Example 1: Let n = 2

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

So the output is:

Smallest perfect 2-digit cube: 27

Largest Perfect 2-Digit Cube: 64

Example 2: Let n = 3

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

So the output is:

Smallest perfect 3-digit cube: 125

Largest Perfect 3-Digit Cube: 729

### Approach to solving the problem

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

``pow(ceil(cbrt(pow(10, (n – 1)))), 3)``

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

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

### C ++ program to find the perfect cubes with N smallest and largest digits

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

``// C++ program to find the smallest and largest// n-digit perfect cubes#include 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;}``

Go out:

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

### Python program for finding the perfect smallest and largest N-digit cubes

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

``# Python program to find the smallest and largest# n-digit perfect cubesimport mathdef 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 = 1print("Number of digits:", n1)findPerfectCubes(n1)n2 = 2print("Number of digits:", n2)findPerfectCubes(n2)n3 = 3print("Number of digits:", n3)findPerfectCubes(n3)n4 = 4print("Number of digits:", n4)findPerfectCubes(n4)``

Go out:

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

### JavaScript program to find the smallest and largest perfect N-digit cubes

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

``// JavaScript program to find the smallest and largest// n-digit perfect cubesfunction 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);``

Go out:

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

Sharpen your brain with challenging math puzzles

If you love solving math puzzles and puzzles, you are doing your brain a favor! Solving puzzles and math puzzles improves memory, increases problem-solving skills, and can increase IQ as well. There are some great websites, YouTube channels, and apps that offer amazing math puzzles and games for free. 