python 冪運算 整數_在Python中檢查一個數字是否是另一個數字的冪

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 冪運算 整數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542690.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542690.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542690.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

3dmax鏡像后模型線條亂了_3dMax入門教程來啦!小白趕緊收藏!

3D Studio Max,常簡稱為3d Max或3ds MAX,是Discreet公司開發的(后被Autodesk公司合并)基于PC系統的三維動畫渲染和制作軟件, 3dmax軟件主要功能有建模,動畫,渲染,特效等,…

java中哲學家就餐死鎖_哲學家就餐問題與死鎖總結

死鎖的四個條件:(1) 互斥條件:一個資源每次只能被一個進程使用。(2) 請求與保持條件:一個進程因請求資源而阻塞時,對已獲得的資源保持不放。(3) 不剝奪條件:進程已獲得的資源,在末使用完之前,不能強行剝奪。…

linux掃描工具之nmap

Linux下有很多強大網絡掃描工具,網絡掃描工具可以分為:主機掃描、主機服務掃描、路由掃描等,nmap支持批量主機掃描和主機服務掃描。檢測安裝:[rootbier ~]# rpm -qa nmap nmap-5.51-4.el6.x86_64如果沒有安裝就安裝一下nmap的安裝直接使用&am…

如何將多個一維列表轉化為二維列表_數據分析2_如何處理一維、二維數據

吞一塊大餅,還不如切成小塊吃得香常見的數據集,要么是數列,要么是表格;因此,數據分析最首要的是,處理一維、二維數據。主要知識點可參考如圖。如需要,可點擊以下百度網盤鏈接下載數據分析基礎知…

關于java中鎖的面試題_Java面試題-Java中的鎖

1. 如何實現樂觀鎖(CAS)?如何避免ABA問題?答:1)讀取內存值的方式實現了樂觀鎖(比如:SVN系統),方法:第一,比較內存值和期望值;第二,替換內存值為要替換值。2)帶參數版本來…

NSUserDefaults

2019獨角獸企業重金招聘Python工程師標準>>> NSUserDefaults 轉載于:https://my.oschina.net/18829297883/blog/737931

什么是算術運算和邏輯運算_8086微處理器的算術和邏輯運算

什么是算術運算和邏輯運算邏輯指令 (Logical Instructions) a) AND: Logical AND a)AND:邏輯AND Atleast one of the operant should be a register or a memory operant both the operant cannot be a memory location or immediate operant. 操作中的至少一個應該…

python文件讀寫用到的庫_Python使用pyshp庫讀取shapefile信息的方法

通過pyshp庫,可以讀寫shapefile文件,查詢相關信息,github地址為 import shapefile # 使用pyshp庫 file shapefile.reader("data\\市界.shp") shapes file.shapes() # print(file.shapetype) # 輸出shp類型null 0 point 1 poly…

h5引入json_Vue中如何使用本地Json文件?

我需要將菜單配置成Json文件,然后再程序中引入{{menu.name}}import menuListConfig from ../../config/menu.jsonexport default {name: "Sider",data(){return {menuList:JSON.parse(JSON.stringify(menuListConfig))}}}需要如何做,才能v-for…

深入學習jQuery選擇器系列第四篇——過濾選擇器之屬性選擇器

前面的話 屬性過濾選擇器的過濾規則是通過元素的屬性來獲取相應的元素,對應于CSS中的屬性選擇器。屬性過濾選擇器可分為簡單屬性選擇器、具體屬性選擇器和條件屬性選擇器三種。本文將詳細該部分內容 簡單屬性選擇器 [attribute] [attribute]選擇器選擇擁有該屬性的元…

c++ scanf讀取_使用scanf()讀取內存地址并在C中打印其值

c scanf讀取Here, we have to input a valid memory address and print the value stored at memory address in C. 在這里,我們必須輸入一個有效的內存地址并在C中打印存儲在內存地址中的值。 To input and print a memory address, we use "%p" format…

python正則匹配_Python正則表達式只匹配一次

我正在嘗試創建一個簡單的降價乳膠轉換器,只是為了學習 python和基本的正則表達式,但我不知道試圖弄清楚為什么下面的代碼不起作用: re.sub (r\[\*\](.*?)\[\*\]: ?(.*?)$, r\\footnote{\2}\1, s, flagsre.MULTILINE|re.DOTALL) 我想轉換像: s "…

Virtual Network (1) - How to use it in a guest

本文將講述一個問題:kvm guest使用libvirt xml定義如何使用virtual network?1)nat, route ,isolated, open類型在host中定義virtual network會創建一個虛擬的bridge,相當于一個交換機。guest只需要連接到這…

java string做除法_如果用java來實現傳統方式的除法,用String來保存結果,想精確多少位都行,那改怎么做?...

我會加分的,提個思路都行,目前做了個乘法和加法,但是現在對除法沒有什么思路。以下是我編寫的功能:publicclassCalculator{publicstaticStringmulti(Strings1,Strings2){if(s1nu...我會加分的,提個思路都行&#xff0c…

c語言數組的聲明和初始化_C聲明和初始化能力問題和解答

c語言數組的聲明和初始化This section contains aptitude questions and answers on C language Declarations and Initialization. 本節包含有關C語言聲明和初始化的適切性問題和解答。 1) What will be the output of following program ? int main(){int m10;int xprintf(…

python2和python3的默認編碼_python2和python3哪個版本新

Python2 還是 Python3 ? py2.7是2.x系列的最后一個版本,已經停止開發,不再增加新功能。2020年終止支持。 所有的最新的標準庫的更新改進,只會在3.x的版本里出現。Python3.0在2008年就發布出來,而2.7作為2.X的最終版本并…

html-css樣式表

一、CSS:Cascading Style Sheet—層疊樣式表,其作用是美化HTML網頁。 樣式表分類:內聯樣式表、內嵌樣式表、外部樣式表 1、內聯樣式表 和HTML聯合顯示,控制精確,但是可重用性差,冗余多。 例如:&…

java 棧 先進后出_棧先進后出,堆先進先出

1.棧(stack)與堆(heap)都是Java用來在Ram中存放數據的地方。與C不同,Java自動管理棧和堆,程序員不能直接地設置棧或堆。2.棧的優勢是,存取速度比堆要快,僅次于直接位于CPU中的寄存器。但缺點是,存在棧中的數據大小與生…

c#給定二維數組按升序排序_在數組中按升序對數字進行排序| 8086微處理器

c#給定二維數組按升序排序Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501. 問題&#xf…

使用python套用excel模板_Python自動化辦公Excel-從表中批量復制粘貼數據到新表

1、模塊安裝 1)cmd模式下: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl 2)如果有安裝Pycharm,則在程序中操作如下: 菜單欄&…