用 while 循环编写一个 Python 程序来检查一个数是否为霓虹数。如果一个数等于其平方数的数字之和,则它是一个霓虹数。例如,9 是,因为 9 的平方是 81,8 + 1 = 9
在这个 Python示例中,我们首先找到一个数的平方。接下来,将该平方数分成单独的数字并找到它们的和。如果和等于原始数字,则它是一个霓虹数。
import math
Number = int(input("Enter the Number to Check Neon Number = "))
Sum = 0
squr = math.pow(Number, 2)
print("Square of a Given Digit = %d" %squr)
while squr > 0:
rem = squr % 10
Sum = Sum + rem
squr = squr // 10
print("The Sum of the Digits = %d" %Sum)
if Sum == Number:
print("\n%d is a Neon Number." %Number)
else:
print("%d is Not a Neon Number." %Number)

使用递归或递归函数编写一个 Python 程序来检查一个数是否为霓虹数。
import math
Sum = 0
def neonNumber(squr):
global Sum
if squr > 0:
rem = squr % 10
Sum = Sum + rem
neonNumber(squr // 10)
return Sum
Number = int(input("Enter the Number to Check Neon Number = "))
squr = math.pow(Number, 2)
print("Square of a Given Digit = %d" %squr)
Sum = neonNumber(squr)
print("The Sum of the Digits = %d" %Sum)
if Sum == Number:
print("\n%d is a Neon Number." %Number)
else:
print("%d is Not a Neon Number." %Number)
Enter the Number to Check Neon Number = 44
Square of a Given Digit = 1936
The Sum of the Digits = 19
44 is Not a Neon Number.
Enter the Number to Check Neon Number = 9
Square of a Given Digit = 81
The Sum of the Digits = 9
9 is a Neon Number.
下面所示的程序将使用 for 循环和 while 循环打印出 1 到 n 之间的霓虹数。
import math
MinNeon = int(input("Please Enter the Minimum = "))
MaxNeon = int(input("Please Enter the Maximum = "))
for i in range(MinNeon, MaxNeon + 1):
Sum = 0
squr = math.pow(i, 2)
while squr > 0:
rem = squr % 10
Sum = Sum + rem
squr = squr // 10
if Sum == i:
print(i, end = ' ')
Please Enter the Minimum = 1
Please Enter the Maximum = 10000
1 9