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 module
import math
# input the numbers
a,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 2
1228 is the power of another number 2.
RUN 2:
Enter two values: 15625 50
15625 is not the power of another number 50.

翻译自: https://www.includehelp.com/python/check-whether-a-number-is-a-power-of-another-number-or-not.aspx

python 幂运算 整数

python 幂运算 整数_在Python中检查一个数字是否是另一个数字的幂相关推荐

  1. python指数运算函数_分享Python中用于计算指数的exp()方法实例教程

    exp()方法返回指数x: ex. 语法 以下是exp()方法的语法:import math math.exp( x ) 注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math ...

  2. python中不同进制的整数之间可以直接运算吗_【python公开课|要想真的做好python,那么学会python整数的不同进制很重要】- 环球网校...

    [摘要]在这个科学技术高速发展的时代,越来越多的人都开始选择学习编程软件,那么首先被大家选择的编程软件就是python,也用在各行各业之中,并被大家所熟知,所以也有越来越多的python学习者关注py ...

  3. python 幂函数 幂为小数_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  4. python画指数函数图像_解决python中的幂函数、指数函数问题

    最近在调代码,碰到幂函数.指数函数,总是提示 ValueError: math domain error ValueError: negative number cannot be raised to ...

  5. python数值运算答案_笨方法学Python 习题3:数字和数学计算

    数字和数学计算 print("I will now count my chickens") print("Hens",25+30/6) print(" ...

  6. 以下选项中python用于异常处理结构_《Python 程序设计》复习题

    目录 填空题 一.基础知识 二.序列 三.选择结构与循环结构和函数及面向对象.文件 选择题 一.Python 基础语法 二.基本数据类型 三.程序的控制结构 四.函数和代码复用 五.组合数据类型 六. ...

  7. python集合的概念_用Python中的集合Set讲解演示高一数学集合的概念

    数学编程不分家,用Python可以方便的表示高中数学知识.现在以集合为例进行讲解,高一数学设计集的概念,讲解了交并补的相关知识,用Python中的集合类型可以方便的表示.同时可以借助数学知识更好的理解 ...

  8. python数组相减_对Python 中矩阵或者数组相减的法则详解

    对Python 中矩阵或者数组相减的法则详解 最近在做编程练习,发现有些结果的值与答案相差较大,通过分析比较得出结论,大概过程如下: 定义了一个计算损失的函数: def error(yhat,labe ...

  9. python字符串转整数_如何在Python中将字符串转换为整数

    Python中的所有数据类型(包括整数和字符串)都是对象.通常在编写Python代码时,您需要将一种数据类型转换为另一种数据类型.例如,要对表示为字符串的数字进行数学运算,需要将其转换为整数. 在本教 ...

最新文章

  1. 【C++】C++11 STL算法(四):二分查找法(Binary search operations)、合并操作
  2. python 序列化函数_python – 如何序列化sympy lambdified函数?
  3. iPhone开发:抢先拥抱软件开发的未来
  4. POJ1679判断最小生成树的唯一性
  5. 【Canal源码分析】TableMetaTSDB
  6. 干货 | 金融级消息队列的演进 — 蚂蚁金服的实践之路
  7. 计算机科学与技术专业导论_教育部最新公布!西安工业大学新增4个本科专业!...
  8. matlab散点拟合系数,matlab离散型数据拟合方程,求系数,哪个大神能说说方法
  9. android 框架LoonAndroid,码农偷懒专用(2014/8/6更新)
  10. 邮件联系人,如何恕不部分字母就能显示邮件联系人
  11. CentOS6.3 重启后/etc/resolv.conf 被还原解决办法
  12. 单调有界定理适用于函数吗_《实变函数》——论有界变差函数
  13. JNI学习-- C调用java方法
  14. 关于SVM参数cg选取的总结帖[matlab-libsvm]
  15. SSD固态硬盘健康状况检测工具SSDReporter
  16. 部署python爬虫_爬虫项目部署
  17. java语言程序设计二级_计算机二级Java语言程序设计试题
  18. word文档 文献尾注修改样式
  19. 网站搜索引擎优化诊断
  20. Matlab实现数字转换为字符串

热门文章

  1. 平板电脑有什么用_除了盖泡面,平板电脑没什么用了
  2. java安全(二):JDBC|sql注入|预编译
  3. python判断语句的复杂度_Python内置方法的时间复杂度(转)
  4. mysql怎么看实例名_南方“中看不中吃”的前4名水果,莲雾只是垫底,你怎么看?...
  5. oracle ora 12011,执行oracle中的job报错:ORA-12011:无法执行作业1
  6. linux课堂笔记(7)
  7. Nginx(七):nginx原理解析
  8. Python数据分析入门(四)
  9. 前端架构设计1:代码核心
  10. 10年老兵给程序员的10条建议!