Python exercises 014: The end of the number

Source: Internet
Author: User

"python exercise 014" a number, if exactly equal to the sum of its factors, this number is called "Finish". such as 6=1+2+3. Programming to find all the finished numbers within 1000.

-----------------------------------------------------------

It is not difficult to understand the concept of "finish number" and its "factor" as long as the problem is understood. At first I understood "factor" as a "quality factor", and the result was only 6. Later, as long as the number a can be divisible by the number B, whether B is a prime, is a factor. For example: 8 of the qualitative factors are 2, 2, 2, but 8 of the factors include 1,2,4.

In this way, the solution "factor" is much simpler than the "quality factor", because there is no need to worry about the quality factor duplication problem. The code is as follows:

Import mathfor I in range (2, £):    factors = []  #因子列表, I empty for    J in range (1, Math.floor (I/2) +1) per loop:        if I%j = = 0:            factors.append (j)    if sum (factors) = = I:        print (i, end= ', ')

The output results are as follows:

6,28,496,

++++++++++++++++++++++++++++++++++++++

Source: Programming Language Introduction Classic 100 Cases "Python Edition"

Python exercises 014: The end of the number

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.