"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