python猜数字程序
Given an integer number and we have to check whether it is perfect number or not?
给定一个整数,我们必须检查它是否为完美数?
This Python program is used to find its all positive divisors excluding that number.
该Python程序用于查找除该数字外的所有正数除数。
Explanation: For Example, 28 is a perfect number since divisors of 28 are 1, 2, 4,7,14 then sum of its divisor is 1 + 2 + 4 + 7 + 14 = 28.
说明:例如,28是一个完美数字,因为28的除数是1、2、4、7、14,然后其除数之和是1 + 2 + 4 + 7 + 14 = 28。
Note: A perfect number is a positive integer that is equal to the sum of its proper positive divisors.
注意:理想数是一个正整数,等于其适当的正因数之和。
Python代码找到完美的数字 (Python code to find perfect number)
if __name__ == "__main__" :
# initialisation
i = 2;sum = 1;
# take input from user and typecast into integer
n = int(input("Enter a number: "))
# iterating till n//2 value
while(i <= n//2 ) :
# if proper divisor then add it.
if (n % i == 0) :
sum += i
i += 1
# check sum equal to n or not
if sum == n :
print(n,"is a perfect number")
else :
print(n,"is not a perfect number")
Output
输出量
First run:
Enter a number: 28
28 is a perfect number
Second run:
Enter a number: 14
14 is not a perfect number
翻译自: https://www.includehelp.com/python/program-to-find-perfect-number.aspx
python猜数字程序