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 3^{rd} position in the series i.e 3^{rd} number which is both a square and a cube.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions