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

0

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: 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 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 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)

Go out:

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

Go out:

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

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: 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 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 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)

Go out:

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

Go out:

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 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.


math-puzzles-riddles-games-featured
5 puzzle resources for free puzzles, riddles and math games

If you like logic puzzles, here is where you can find more amazing math puzzles and games to sharpen your mind.

Read more


About the Author


Source link

Share.

About Author

Leave A Reply