博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 幂运算 整数_在Python中检查一个数字是否是另一个数字的幂
阅读量:2530 次
发布时间:2019-05-11

本文共 2103 字,大约阅读时间需要 7 分钟。

python 幂运算 整数

To solve this problem simply, we will use the log() function from the math module. The math module provides us various mathematical operations and here we will use the log() function from this module. In Python working of log() function, is the same as log work in mathematics. Here, the user will provide us two positive values a and b and we have to check whether a number is a power of another number or not in Python. The idea is simple to find the log of a base b and takes the integer part of it and assigns it to a variable s. After this just check if s to the power of b is equal to a then a is the power of another number b. Before going to solve this, we will see the algorithm to solve this problem and try to understand it.

为了简单地解决这个问题,我们将使用math模块中log()函数 。 数学模块为我们提供了各种数学运算,在这里我们将使用该模块中的log()函数 。 在Python中, log()函数的工作方式与数学中的日志工作相同。 在这里,用户将为我们提供两个正值a和b ,我们必须检查一个数字是否是Python中另一个数字的幂 。 这个想法是简单找到一个基极b的对数,并采取的它并给它分配的整数部分为变量s。 之后,只需检查s的b的幂是否等于a,则a是另一个数字b 。 在解决此问题之前,我们将看到解决该问题的算法并尝试理解它。

Algorithm to solve this problem:

解决此问题的算法:

  1. Initially, we will import the math module in the program.

    最初,我们将把数学模块导入程序中。

  2. Takes the positive value of a and b from the user.

    从用户处获得a和b的正值。

  3. Find the log of a base b and assign its integer part to variable s.

    找到一个基极b的对数,并分配其整数部分到变量s。

  4. Also, find the b to the power s and assign it to another variable p.

    同样,找到b的幂s并将其分配给另一个变量p 。

  5. Check if p is equal to a then a is a power of another number b and print a is the power of another number b.

    检查p是否等于a,那么a是另一个数字b的幂,而print a是另一个数字b的幂。

Now, we will write the Python program by the implementation of the above algorithm.

现在,我们将通过上述算法的实现编写Python程序。

Program:

程序:

# importing the moduleimport math# input the numbersa,b=map(int,input('Enter two values: ').split())s=math.log(a,b)p=round(s)if (b**p)==a:    print('{} is the power of another number {}.'.format(a,b))else:    print('{} is not the power of another number {}.'.format(a,b))

Output

输出量

RUN 1:Enter two values: 1228 21228 is the power of another number 2.	RUN 2:Enter two values: 15625 5015625 is not the power of another number 50.

翻译自:

python 幂运算 整数

转载地址:http://izxzd.baihongyu.com/

你可能感兴趣的文章
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>