1 #题目: If a number is exactly equal to the sum of its factors, this number is called the "end number". For example, 6=1+2+3. Programming to find all the finished numbers within 1000.
Code:
2 3 for I in Range (1,1001): 4 s = 0 5 for j in Range (1,i): 6 if I%j = = 0:7 S + = J 8 if s = = I:9 print (i)
Operation Result:
[[email protected] code_100]# python code_19.py 628496[[email protected] code_100]#
Code Explanation:
2 3 for i in range (1,1001): #遍历1到1000所有的数字 4 s = 0 #将约数的和置0, this assignment cannot be outside for, and the outer for Each loop once, reset 5 for j in range (1,i): #遍历1到 (i-1) All the numbers, the last number is out of the number before I itself, approximate from these numbers to take 6 if i%j == 0: #将i和j进行取余操作, the remainder of 0 is the approximate of I 7 s += j #将约数j累加, and for s 8 if s == i: #如果和与i相等 (this if statement is to be placed outside the inner for loop, otherwise the approximate sum of incomplete, such as 24, the number must be added to the total number of the offer and I compare equal) 9 print (i) #这个i就是一个完全数, print this number
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1747580
"Python" programming language Introduction Classic 100 cases--19