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: 1
Smallest 1-digit excellent sq.: 1
Largest 1-digit excellent sq.: 9
Variety of digits: 2
Smallest 2-digit excellent sq.: 16
Largest 2-digit excellent sq.: 81
Variety of digits: 3
Smallest 3-digit excellent sq.: 100
Largest 3-digit excellent sq.: 961
Variety of digits: 4
Smallest 4-digit excellent sq.: 1024
Largest 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 squares
import math
def 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 = 1
print("Variety of digits:", n1)
findPerfectSquares(n1)
n2 = 2
print("Variety of digits:", n2)
findPerfectSquares(n2)
n3 = 3
print("Variety of digits:", n3)
findPerfectSquares(n3)
n4 = 4
print("Variety of digits:", n4)
findPerfectSquares(n4)

Output:

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

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

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 squares
perform 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: 1
Smallest 1-digit excellent sq.: 1
Largest 1-digit excellent sq.: 9
Variety of digits: 2
Smallest 2-digit excellent sq.: 16
Largest 2-digit excellent sq.: 81
Variety of digits: 3
Smallest 3-digit excellent sq.: 100
Largest 3-digit excellent sq.: 961
Variety of digits: 4
Smallest 4-digit excellent sq.: 1024
Largest 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: 1
Smallest 1-digit excellent dice: 1
Largest 1-digit excellent dice: 8
Variety of digits: 2
Smallest 2-digit excellent dice: 27
Largest 2-digit excellent dice: 64
Variety of digits: 3
Smallest 3-digit excellent dice: 125
Largest 3-digit excellent dice: 729
Variety of digits: 4
Smallest 4-digit excellent dice: 1000
Largest 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 cubes
import math
def 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 = 1
print("Variety of digits:", n1)
findPerfectCubes(n1)
n2 = 2
print("Variety of digits:", n2)
findPerfectCubes(n2)
n3 = 3
print("Variety of digits:", n3)
findPerfectCubes(n3)
n4 = 4
print("Variety of digits:", n4)
findPerfectCubes(n4)

Output:

Variety of digits: 1
Smallest 1 -digit excellent dice: 1
Largest 1 -digit excellent dice: 8
Variety of digits: 2
Smallest 2 -digit excellent dice: 27
Largest 2 -digit excellent dice: 64
Variety of digits: 3
Smallest 3 -digit excellent dice: 125
Largest 3 -digit excellent dice: 729
Variety of digits: 4
Smallest 4 -digit excellent dice: 1000
Largest 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 cubes
perform 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: 1
Smallest 1-digit excellent dice: 1
Largest 1-digit excellent dice: 8
Variety of digits: 2
Smallest 2-digit excellent dice: 27
Largest 2-digit excellent dice: 64
Variety of digits: 3
Smallest 3-digit excellent dice: 125
Largest 3-digit excellent dice: 729
Variety of digits: 4
Smallest 4-digit excellent dice: 1000
Largest 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.


math-puzzles-riddles-games-featured
5 Brain-Teasing Resources for Free Math Puzzles, Riddles, and Games

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

Read Next


About The Writer

https://www.makeuseof.com/find-smallest-and-largest-ndigit-perfect-cubes-squares/ | Discover N-Digit Excellent Cubes and Squares Utilizing Python, C++, and JavaScript

Sportset

Fry Electronics is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@fry-electronics.com. The content will be deleted within 24 hours.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

nineteen − six =

Back to top button