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 红包祝福语简短10字
18 周六早晨祝福语简短