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 生日祝福语 男性简短独特