Home » Python » Python programs

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?
Submitted by Bipin Kumar, on November 08, 2019

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

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

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.