📜  Python的强数

📅  最后修改于: 2020-10-29 01:04:05             🧑  作者: Mango

Python的强数

在本教程中,我们将学习一个Python程序来查找给定的数字是否为强数。

什么是强数?

强数是一个特殊数字,其所有数字阶乘的和应等于数字本身。

查找给定数字是否强。我们从给定的数字中选择每个数字并找到其阶乘,然后将数字的每个数字都这样做。

一旦获得所有数字的阶乘,就可以进行阶乘之和。如果总和等于给定数字,则给定数字很强,否则就不强。

例如-给定的数字为145,我们必须选择每个数字并找到阶乘1! = 1,4! = 24和5! = 120。

现在,我们将对阶乘进行求和,得到1 + 24 + 120 = 145,这与给定的数字完全相同。因此,可以说145是一个强大的数字。

我们得到了强数的逻辑。现在使用Python程序实现它。

问题方法

Python程序寻找强数

下面是print给定数字是否强的Python程序代码。

范例-

# Variable to store sum of the numbers
sum=0
# Ask user to enter the number
num=int(input("Enter a number:"))
# temporary variable  store copy of the original number
temp=num
# Using while loop
while(num):
    # intialize with 1
    i=1
    # fact variable with 1
    fact=1
    rem=num%10
    while(i<=rem):
        fact=fact*i   # Find factorial of each number
        i=i+1
    sum=sum+fact
    num=num//10
if(sum==temp):
    print("Given number is a strong number")
else:
    print("Given number is not a strong number")

输出:

Enter a number: 145
Given number is a strong number.

说明:

在上面的代码中

  • 我们声明了一个可变整数值num以输入数字。
  • 定义总和变量为零。
  • num值到temp变量的副本。
  • 在第一个while循环中,确保给定的数字大于0。
  • 在while循环内,分割数字并分配变量以查找每个数字的阶乘。
  • 在第二个while循环(嵌套while循环)中,找到每个数字的阶乘。

假设用户输入值= 145且总和= 0

分配初始值

i = 0
fact = 0
temp = num
temp = 145

现在了解循环迭代。第一次迭代

rem = temp % 10
rem = 145 % 10 = 5

现在,我们进入嵌套的while循环。它计算5的阶乘为120。

sum = sum + 120> 0+120
sum = 120
temp = temp//10 = 14
temp = 14

第二次迭代

temp = 14,
sum = 120
rem = 14 % 10 = 4

现在,它进入嵌套的While循环。在这里,它计算4 = 24的阶乘。

sum = 120 + 24
sum = 144

temp = 14//10   
temp = 1

第三次迭代

temp = 1 
sum = 144
rem = 1 % 10 = 0

1的阶乘为1

sum = 144 + 1
sum = 145
temp = 1 / 10
temp = 0

在这里,temp = 0,因此,while循环条件失败。

如果(num == sum)现在,我们检查条件,即用户输入的数字是否完全等于sum。如果此条件返回True,则为强数,否则为非强数。

我们使用while循环完成了程序。我们还可以使用for循环来查找给定数字是否强。

强数使用for循环

我们也可以使用for循环找到强数。逻辑与上述程序相同,while循环被for循环替换。

范例-

# Python Program to find Strong Number
num = int(input(" Enter the Number:"))
sum = 0
temp = num

while(temp > 0):
    fact = 1
    rem = temp % 10

    for i in range(1, rem + 1):
        fact = fact * i

    print("Factorial of %d = %d" %(rem, fact))
    sum = sum + fact
    temp = temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))
    
if (sum == num):
    print(" The given number is a Strong Number")
else:
    print(" The given number is not a Strong Number")

输出:

Enter the Number:145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of Factorials of a Given Number 145 = 145
The given number is a Strong Number

Python程序使用阶乘函数查找强数

Python数学模块提供了内置的数学模块。通过使用此方法,我们可以省略嵌套while循环的使用。

范例-

# Python Program to find Strong Number

import math
num = int(input(" Enter the Number:"))
sum = 0
temp = num

while(temp > 0):
    rem = temp % 10
    fact = math.factorial(rem)  # Using the buitlt-in factorial() function

    print("Factorial of %d = %d" %(rem, fact))
    sum = sum + fact
    temp = temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))
    
if (sum == num):
    print(" The given number is a Strong Number")
else:
    print(" The given number is not a Strong Number") 

输出:

Enter the Number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1

 Sum of Factorials of a Given Number 145 = 145
 The given number is a Strong Number

说明-

在上面的代码中,

  • 我们使用了factorial()函数并传递了一个提醒作为参数。
  • 在while循环的第一次迭代中,它返回提醒5并传递给阶乘5。
  • 它将持续到温度值大于零为止。我们不需要使用另一个while循环。