Technology

# How to Find N-Digit Perfect Cubes and Squares Using Python, C++, and JavaScript

Many programmers love fixing tough mathematical issues utilizing code. It helps sharpen the thoughts and enhance problem-solving expertise. On this article, you may learn to discover the smallest and largest n-digit excellent squares and cubes utilizing Python, C++, and JavaScript. Every instance additionally accommodates pattern output for a number of completely different values.

## Smallest and Largest N-Digit Excellent Squares

### Downside Assertion

You are given an integer n, and you might want to discover the smallest and largest n-digit numbers which are additionally excellent squares.

Instance 1: Let n = 2

Smallest 2-digit excellent sq. is 16 and the most important 2-digit excellent sq. is 81.

Thus, the output is:

Smallest 2-digit excellent sq.: 16

Largest 2-digit excellent sq.: 81

Instance 2: Let n = 3

Smallest 3-digit excellent sq. is 100 and the most important 3-digit excellent sq. is 961.

Thus, the output is:

Smallest 3-digit excellent sq.: 100

Largest 3-digit excellent sq.: 961

### Strategy to Clear up the Downside

Yow will discover the smallest n-digit excellent sq. utilizing the next components:

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

And to seek out the most important n-digit excellent sq., use the next components:

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

### C++ Program to Discover the Smallest and Largest N-Digit Excellent Squares

Under is the C++ program to seek out the smallest and largest n-digit excellent squares:

``// C++ program to seek out the smallest and largest// n-digit excellent squares#embody <bits/stdc++.h>utilizing namespace std;void findPerfectSquares(int n){cout << "Smallest "<< n << "-digit excellent sq.: " << pow(ceil(sqrt(pow(10, n - 1))), 2) << endl;cout << "Largest " << n << "-digit excellent sq.: " << pow(ceil(sqrt(pow(10, n))) - 1, 2) << endl;}int foremost(){int n1 = 1;cout << "Variety of digits: " << n1 << endl;findPerfectSquares(n1);int n2 = 2;cout << "Variety of digits: " << n2 << endl;findPerfectSquares(n2);int n3 = 3;cout << "Variety of digits: " << n3 << endl;findPerfectSquares(n3);int n4 = 4;cout << "Variety of digits: " << n4 << endl;findPerfectSquares(n4);return 0;}``

Output:

``Variety of digits: 1Smallest 1-digit excellent sq.: 1Largest 1-digit excellent sq.: 9Variety of digits: 2Smallest 2-digit excellent sq.: 16Largest 2-digit excellent sq.: 81Variety of digits: 3Smallest 3-digit excellent sq.: 100Largest 3-digit excellent sq.: 961Variety of digits: 4Smallest 4-digit excellent sq.: 1024Largest 4-digit excellent sq.: 9801``

Associated: How to Calculate the Value of nCr

### Python Program to Discover the Smallest and Largest N-Digit Excellent Squares

Under is the Python program to seek out the smallest and largest n-digit excellent squares:

``# Python program to seek out the smallest and largest# n-digit excellent squaresimport mathdef findPerfectSquares(n):print("Smallest ", n,"-digit excellent sq.:", pow(math.ceil(math.sqrt(pow(10, n - 1))), 2))print("Largest ", n,"-digit excellent sq.:", pow(math.ceil(math.sqrt(pow(10, n))) - 1, 2))n1 = 1print("Variety of digits:", n1)findPerfectSquares(n1)n2 = 2print("Variety of digits:", n2)findPerfectSquares(n2)n3 = 3print("Variety of digits:", n3)findPerfectSquares(n3)n4 = 4print("Variety of digits:", n4)findPerfectSquares(n4)``

Output:

``Variety of digits: 1Smallest  1 -digit excellent sq.: 1Largest  1 -digit excellent sq.: 9Variety of digits: 2Smallest  2 -digit excellent sq.: 16Largest  2 -digit excellent sq.: 81Variety of digits: 3Smallest  3 -digit excellent sq.: 100Largest  3 -digit excellent sq.: 961Variety of digits: 4Smallest  4 -digit excellent sq.: 1024Largest  4 -digit excellent sq.: 9801``

### JavaScript Program to Discover the Smallest and Largest N-Digit Excellent Squares

Under is the JavaScript program to seek out the smallest and largest n-digit excellent squares:

``// JavaScript program to seek out the smallest and largest// n-digit excellent squaresperform findPerfectSquares(n) {doc.write("Smallest " + n + "-digit excellent sq.: " + Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n - 1))), 2) + "<br>");doc.write("Largest " + n + "-digit excellent sq.: " + Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n))) - 1, 2) + "<br>");}var n1 = 1;doc.write("Variety of digits: " + n1 + "<br>");findPerfectSquares(n1);var n2 = 2;doc.write("Variety of digits: " + n2 + "<br>");findPerfectSquares(n2);var n3 = 3;doc.write("Variety of digits: " + n3 + "<br>");findPerfectSquares(n3);var n4 = 4;doc.write("Variety of digits: " + n4 + "<br>");findPerfectSquares(n4);``

Output:

``Variety of digits: 1Smallest 1-digit excellent sq.: 1Largest 1-digit excellent sq.: 9Variety of digits: 2Smallest 2-digit excellent sq.: 16Largest 2-digit excellent sq.: 81Variety of digits: 3Smallest 3-digit excellent sq.: 100Largest 3-digit excellent sq.: 961Variety of digits: 4Smallest 4-digit excellent sq.: 1024Largest 4-digit excellent sq.: 9801``

## Smallest and Largest N-Digit Excellent Cubes

### Downside Assertion

You are given an integer n, you might want to discover the smallest and largest n-digit numbers which are additionally excellent cubes.

Instance 1: Let n = 2

Smallest 2-digit excellent dice is 27 and the most important 2-digit excellent dice is 64.

Thus, the output is:

Smallest 2-digit excellent dice: 27

Largest 2-digit excellent dice: 64

Instance 2: Let n = 3

Smallest 3-digit excellent dice is 120 and the most important 3-digit excellent dice is 729.

Thus, the output is:

Smallest 3-digit excellent dice: 125

Largest 3-digit excellent dice: 729

### Strategy to Clear up the Downside

Yow will discover the smallest n-digit excellent dice utilizing the next components:

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

And to seek out the most important n-digit excellent dice, use the next components:

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

### C++ Program to Discover the Smallest and Largest N-Digit Excellent Cubes

Under is the C++ program to seek out the smallest and largest n-digit excellent cubes:

``// C++ program to seek out the smallest and largest// n-digit excellent cubes#embody <bits/stdc++.h>utilizing namespace std;void findPerfectCubes(int n){cout << "Smallest "<< n << "-digit excellent dice: " << pow(ceil(cbrt(pow(10, (n - 1)))), 3) << endl;cout << "Largest " << n << "-digit excellent dice: " << (int)pow(ceil(cbrt(pow(10, (n)))) - 1, 3) << endl;}int foremost(){int n1 = 1;cout << "Variety of digits: " << n1 << endl;findPerfectCubes(n1);int n2 = 2;cout << "Variety of digits: " << n2 << endl;findPerfectCubes(n2);int n3 = 3;cout << "Variety of digits: " << n3 << endl;findPerfectCubes(n3);int n4 = 4;cout << "Variety of digits: " << n4 << endl;findPerfectCubes(n4);return 0;}``

Output:

``Variety of digits: 1Smallest 1-digit excellent dice: 1Largest 1-digit excellent dice: 8Variety of digits: 2Smallest 2-digit excellent dice: 27Largest 2-digit excellent dice: 64Variety of digits: 3Smallest 3-digit excellent dice: 125Largest 3-digit excellent dice: 729Variety of digits: 4Smallest 4-digit excellent dice: 1000Largest 4-digit excellent dice: 9261``

### Python Program to Discover the Smallest and Largest N-Digit Excellent Cubes

Under is the Python program to seek out the smallest and largest n-digit excellent cubes:

``# Python program to seek out the smallest and largest# n-digit excellent cubesimport mathdef findPerfectCubes(n):print("Smallest ", n,"-digit excellent dice:", pow(math.ceil((pow(10, (n - 1))) ** (1 / 3)), 3) )print("Largest ", n,"-digit excellent dice:", pow(math.ceil((pow(10, (n))) ** (1 / 3)) - 1, 3))n1 = 1print("Variety of digits:", n1)findPerfectCubes(n1)n2 = 2print("Variety of digits:", n2)findPerfectCubes(n2)n3 = 3print("Variety of digits:", n3)findPerfectCubes(n3)n4 = 4print("Variety of digits:", n4)findPerfectCubes(n4)``

Output:

``Variety of digits: 1Smallest  1 -digit excellent dice: 1Largest  1 -digit excellent dice: 8Variety of digits: 2Smallest  2 -digit excellent dice: 27Largest  2 -digit excellent dice: 64Variety of digits: 3Smallest  3 -digit excellent dice: 125Largest  3 -digit excellent dice: 729Variety of digits: 4Smallest  4 -digit excellent dice: 1000Largest  4 -digit excellent dice: 9261``

### JavaScript Program to Discover the Smallest and Largest N-Digit Excellent Cubes

Under is the JavaScript program to seek out the smallest and largest n-digit excellent cubes:

``// JavaScript program to seek out the smallest and largest// n-digit excellent cubesperform findPerfectCubes(n) {doc.write("Smallest " + n + "-digit excellent dice: " + Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n - 1)))), 3) + "<br>");doc.write("Largest " + n + "-digit excellent dice: " + Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n)))) - 1, 3) + "<br>");}var n1 = 1;doc.write("Variety of digits: " + n1 + "<br>");findPerfectCubes(n1);var n2 = 2;doc.write("Variety of digits: " + n2 + "<br>");findPerfectCubes(n2);var n3 = 3;doc.write("Variety of digits: " + n3 + "<br>");findPerfectCubes(n3);var n4 = 4;doc.write("Variety of digits: " + n4 + "<br>");findPerfectCubes(n4);``

Output:

``Variety of digits: 1Smallest 1-digit excellent dice: 1Largest 1-digit excellent dice: 8Variety of digits: 2Smallest 2-digit excellent dice: 27Largest 2-digit excellent dice: 64Variety of digits: 3Smallest 3-digit excellent dice: 125Largest 3-digit excellent dice: 729Variety of digits: 4Smallest 4-digit excellent dice: 1000Largest 4-digit excellent dice: 9261``

## Sharpen Your Mind With Stimulating Math Puzzles

In the event you’re somebody who loves fixing math puzzles and riddles, you are doing all your mind a favor! Fixing math puzzles and riddles improves reminiscence, will increase problem-solving expertise, and may enhance IQ. Some nice web sites, YouTube channels, and apps present superb math puzzles and video games without spending a dime.

If you love logic puzzles, here’s where you can get some more amazing math riddles and games to sharpen your wits.