3

Python编程3——Python计算是否是perfect number

 3 years ago
source link: https://iphyer.github.io/blog/2013/01/13/pythonperfectnumber/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python编程3——Python计算是否是perfect number

这里是实现0~100之内数字输出全部的完美数,所谓完美数也就是这个数的所有因子的和等于这个数。

6=1+2+3

28=1+2+4+7+14

#the programe is judging whether the number is perfect
#the perfect number is number that the sum of its factor equals itself
#like 6=1+2+3,28=1+2+4+7=14

topNum=raw_input("Please enter the upper number of the range: \n")
topNum=int(topNum)
theNum=2


while theNum<=topNum:
	#sum up all the factor
	divisor=1
	sumofDivisors=0
	#judge whether it is the factors of theNum
	while divisor<theNum:
		if theNum%divisor==0:
			sumofDivisors=sumofDivisors+divisor
		divisor=divisor+1
	if theNum==sumofDivisors:
		print theNum,"is perfect"
	theNum+=1

当然可以看出来100以内也就只有6和28两个数字。

Written on January 13, 2013

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK