Python 程序:打印 1 到 100 的强数

编写一个 Python 程序,打印 1 到 100、1 到 n 或最小值到最大值的强数,并附带示例。

Python 程序:打印 1 到 100 的强数

此 Python 程序 允许用户输入最大限制值。然后,此程序将打印从 1 到用户输入值之间的强数。在此 Python 程序 中,我们首先使用 For Loop 来迭代从 1 到最大值之间的循环。在 Python for 循环中,

  • 我们使用 While Loop 来拆分给定的数字。以便我们可以找到数字中每个数字的阶乘。
  • 在 While 循环中,我们使用 factorial 函数来 查找阶乘
  • 该 if statement 通过将原始值与阶乘之和进行比较,来检查给定的数字是否为强数。

提示:我建议您参考 Factorial 和 Strong Number 文章来理解 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 Program to print Strong Numbers from 1 to 100

Python 程序:打印 1 到 N 的强数

在此程序中,我们允许用户输入最小值和最大值。接下来,此 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