Q:

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

belongs to collection: Python basic programs

0

 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. 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 to solve this problem:

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

N = int(input('Enter the value of N: '))
R = N**6
print('Nth number: ',R)

Output

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Program to find the execution time of a program in... >>
<< Print the all uppercase and lowercase alphabets in...