0

Hello I need your help:D Can anyone explain this code ?

a= ; #fill in the blanks! roots=[x for x in range(1,a) if a%x ==o]; print(sum(roots)==a and a>0

15th May 2017, 6:19 PM
Soubhi
2 Answers
+ 8
Looks like a perfect number checker to me. But needs some fixing ;) a = 6 # try 6, 28, 496 or 8128 roots = [x for x in range(1, a) if a%x == 0] print(sum(roots) == a and a > 0) roots is constructed by a list comprehension - this is the expression inside the square brackets. It tells Python to create this list by iterating from 1 to a (6 in this example) and an only those elements which a is divisible by (so the remainder of such a division a%x is zero) - 1, 2 and 3 in our example. Then it prints a result of a logical test of: 1. sum of roots elements equals to a and 2. a > 0 This logical test will be True only if a is a perfect number (the definition is that a perfect number is equal to the sum of all its divisors but the number itself). You can check out my code on perfect numbers, too :) https://code.sololearn.com/cz9goi77Ls5L/?ref=app
15th May 2017, 7:43 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
0
thank you, that was very helpful!
16th May 2017, 10:41 AM
Soubhi