写一个 Python 程序,打印 1 到 100 或一定范围内的幸福数。这个 Python 示例接受最小值和最大值,并显示该范围内的幸福数。
import math
def digitsSquareSum(Number):
Sum = rem = 0
while Number > 0:
rem = Number % 10
Sum = Sum + math.pow(rem, 2)
Number = Number // 10
return Sum
minHpy = int(input("Enter the Minimum Happy Number = "))
maxHpy = int(input("Enter the Maximum Happy Number = "))
print("\nThe List of Happy Numbers from {0} and {1}".format(minHpy, maxHpy))
for i in range(minHpy, maxHpy + 1):
Temp = i
while Temp != 1 and Temp != 4:
Temp = digitsSquareSum(Temp)
if Temp == 1:
print(i, end = ' ')
原创文章,作者:RY3E8,如若转载,请注明出处:https://www.506064.com/n/127522.html