ITPub博客

首页 > IT职业 > IT图书 > Python实现水仙花数

Python实现水仙花数

IT图书 作者:usli 时间:2021-09-09 09:29:21 0 删除 编辑

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)《摘自百度百科》。

下面给出三位数水仙花Python代码实现::

# 循环遍历出所有三位数for tmp in range(100, 1000):    # 取余找出个位数    a = tmp % 10    # 求商取整找出百位数    b = int(tmp / 100)    # 通过求商取整找出百位和十位,然后求商找出十位    c = int(tmp / 10) % 10    if tmp == a**3 + b**3 + c**3:        print("%d" %tmp)

有兴趣可以百度百科了解下https://baike.baidu.com/item/%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B0/2746160?fr=aladdin>;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3324/viewspace-2803617/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    89
  • 访问量
    808