python中第n个数字
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.
在此程序中,用户将提供数字N ,我们必须找到第N个数字,该数字既是square又是cube 。 一些同时为正方形和立方的数字的示例是1、64、729等。您会想到一种简单的方法,即制作一个既是正方形又是立方的数字列表,并使用列表的索引找到第n个数字但是解决这些问题的这些方法将花费大量时间,并且可能显示超过了时间限制。 因此,为了克服这些问题,我们将使用数学方法以简单的方式解决该问题,该方法仅在给定数字的六次方中可以找到。
Algorithm to solve this problem:
解决此问题的算法:
Take input from the user i.e value of N.
接受用户输入,即N的值。
Find the N-th power of the given number N and assign it to a new variable R.
找出给定数字N的N次幂并将其分配给新变量R。
Print the variable R which is our Nth number.
打印变量R ,这是我们的第N个数字。
So, let's try to solve the problem by the implementation of the above algorithm in Python.
因此,让我们尝试通过在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.
在Python中,双星号( ** )用于查找数字的幂。
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.
729是27的平方,而9的立方也是64是8的平方和4的立方。 当我们在自然数检查类似这样的数字,然后我们将得到一个系列的第1,64729,4096等,这里729是在一系列的第三位置,即第3号这既是一个正方形和一个立方体。
python中第n个数字