Python 中的强数

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

什么是强数?

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

找出给定的数是否强。我们从给定的数字中挑选每个数字,并找到它的阶乘,我们将对数字的每个数字都这样做。

一旦我们得到了所有数字的阶乘,那么我们就得到阶乘的和。如果和等于给定的数,那么给定的数是强的,否则不是。

比如- 给定的数字是 145,我们要挑每个数字,求阶乘 1!= 1, 4!= 24 和 5!= 120.

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

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

问题处理方式

  1. 要求用户输入一个整数。
  2. 使用 While循环找到数字中每个数字的阶乘。
  3. 现在,总结所有的阶乘数。
  4. 检查它是否等于给定的数字。
  5. 打印输出。
  6. 出口

样本输入:数= 132

样本输出:给定的数字不是一个强数

说明: 1!+ 3!+ 2!= 9,不等于 132

样本输入:数= 145

样本输出:给定的数字是一个强数。

Python 程序:寻找强数

下面是 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 值的副本。
  • 在第一个 While循环中,确保给定的数字大于 0。
  • While循环中,拆分数字并分配变量以找到每个数字的阶乘。
  • 在第二个 While循环(嵌套 While循环)中,找到每个数字的阶乘。

假设用户输入值= 145,sum = 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

这里温度= 0,所以 While循环条件失败。

如果(num == sum)现在,我们检查用户输入的数字是否正好等于 sum。如果这个条件返回真,那么它是强数,否则它不是强数。

我们已经使用 While循环完成了程序。我们也可以使用进行循环来查找给定的数字是否强。

使用 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 math模块提供内置的math模块。通过使用这种方法,我们可以省略使用嵌套 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

解释-

在上面的代码中,

  • 我们使用了阶乘()函数,并传递了一个提醒作为参数。

  • While循环的第一次迭代中,它返回提醒 5 并传递给阶乘 5。

  • 它将一直持续到温度值大于零。我们不需要使用另一个 While循环。

原创文章,作者:O5HM0,如若转载,请注明出处:https://www.506064.com/n/129819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
O5HM0的头像O5HM0
上一篇 2024-10-03 23:27
下一篇 2024-10-03 23:27

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29

发表回复

登录后才能评论