"Python" programming language Introduction Classic 100 cases--19

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.