寫一個 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/zh-tw/n/127522.html