Python中的阿姆斯特朗数
假设我们有一个k位数字N。当每个数字的k次幂加到N时,N是一个Armstrong数字。因此,如果它是Armstrong数字,则必须返回true,否则返回false。
为了解决这个问题,我们将遵循以下步骤-
幂:=数字位数
温度:=n,res=0
当温度不为0时
res:=res+(tempmod10)^幂
temp:=temp/10//整数除法
如果res=n,则返回true,否则返回false。
示例
让我们看下面的实现以更好地理解-
import math
class Solution(object):
def poww(self,base,power):
res = 1
while power:
if power & 1:
res *= base
base *= base
power>>=1
return res
def isArmstrong(self, n):
power =int(math.log10(n)) + 1
temp = n
res = 0
while temp:
res += (self.poww(temp%10,power))
temp//=10
return res == n
ob1 = Solution()
print(ob1.isArmstrong(153))输入值
153
输出结果
true
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短