写一个 Python示例来打印从 1 到 100,或 1 到 n,或最小到最大的强数,并举例说明。
这个 python 程序允许用户输入最大限值。接下来,该程序打印从 1 到用户输入值的强数。在这个 python 程序中,首先,我们使用 For Loop 来迭代一个介于 1 和最大值之间的循环。 蟒内为回路
提示:建议大家参考阶乘、强数文章,了解 Python 逻辑。
# Python Program to print Strong Numbers from 1 to N
import math
maximum = int(input(" Please Enter the Maximum Value: "))
for Number in range(1, maximum):
Temp = Number
Sum = 0
while(Temp > 0):
Reminder = Temp % 10
Factorial = math.factorial(Reminder)
Sum = Sum + Factorial
Temp = Temp // 10
if (Sum == Number):
print(" %d is a Strong Number" %Number)
在这个程序中,我们允许用户输入最小值和最大值。接下来,这个 Python示例打印介于最小值和最大值之间的强数
import math
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
for Number in range(minimum, maximum):
Temp = Number
Sum = 0
while(Temp > 0):
Reminder = Temp % 10
Factorial = math.factorial(Reminder)
Sum = Sum + Factorial
Temp = Temp // 10
if (Sum == Number):
print(" %d is a Strong Number" %Number)
Please Enter the Minimum Value: 10
Please Enter the Maximum Value: 100000
145 is a Strong Number
40585 is a Strong Number
标签: 智能AI