Find the N-th number which is both square and a cube of a number in Python

Here, we will learn how to find the N-th number which is both square and cube in the Python programming language? By Bipin Kumar Last updated : January 05, 2024

Problem statement

In this program, a number N will be provided by the user and we have to find the N-th number which is both square and cube.

Example of some numbers which are both square and cube are 1, 64, 729, etc.

Solution approach

A simple approach will come to your mind that makes a list of numbers which is both square and cube and by using the indexing of list find the nth number but these approaches to the solution of this problem will take a lot of time and it may be shown time limit exceeded. So, to overcome these problems we will use the mathematical approach for solving this problem in a simple way which is just found in the 6th power of the given number.

Algorithm

Algorithm to find the N-th number which is both square and a cube of a number is:

  • Take input from the user i.e value of N.
  • Find the N-th power of the given number N and assign it to a new variable R.
  • Print the variable R which is our Nth number.

So, let's try to solve the problem by the implementation of the above algorithm in Python.

Python program to find the N-th number which is both square and a cube of a number

# Input an integer number
N = int(input("Enter the value of N: "))

# Performing the formula
R = N**6

# printing the nth number
print("Nth number: ", R)

Output

The output of the above example is:

RUN 1:
Enter the value of N: 3
Nth number: 729

RUN 2:
Enter the value of N: 2
Nth number: 64

In Python, a double asterisk (**) is used to find the power of a number.

Explanation

729 is a square of 27 and a cube of 9 also 64 is a square of 8 and a cube of 4. When we check numbers like this in the natural number then we will get a series of 1, 64,729, 4096, etc. and here 729 is at 3rd position in the series i.e 3rd number which is both a square and a cube.

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.