-1

everyone. I have a question about an exercise from Brian Heinold's A Practical Introduction to Python Programming that reads "A number is called a perfect number if it is equal to the sum of all of its divisors, not including the number itself. For instance, 6 is a perfect number because the divisors of 6 are 1, 2, 3, 6 and 6 = 1 + 2 + 3. As another example, 28 is a perfect number because its divisors are 1, 2, 4, 7, 14, 28 and 28 = 1 + 2 + 4 + 7 + 14. However, 15 is not a perfect number because its divisors are 1, 3, 5, 15 and 15 ̸= 1 + 3 + 5. Write a program that finds all four of the perfect numbers."

I'm a beginner. I tried. My code doesn't work. Please tell me where did I go wrong and why does the program print the number 1 endlessly why I press Run.

Thank you.

# We have to check all numbers from 1 to 10000.
for i in range(1,10001):
    # Since all numbers are divisible by 1,
    #we can set 1 as the initial value.
    sum_div = 1
    #The potential divisors also range from 1 to 10000,
    #therefore we can use this nested loop:
    for j in range(1,20001):
        # A j value can be a divisor if the remainder is zero.
        # AND the range of the divisors must not include the number itself.
        # Since 1 is already known to be a divisor, we can start checking from 2.
        if i % j == 0 and j != i:
            #We already have 1 as the first divisor,
            #so now we have to add the other divisors.
            sum_div = sum_div + j
        #If the sum of the divisors equals the number,
        #then we got the number we need.
        if sum_div == i:
            print(i)

2 Answers2

0

The j for loop should be from 2 to i-1 not 1 to 20001 and your if logic should be outside j for loop indicating we are done with counting sum

# We have to check all numbers from 1 to 10000.
for i in range(1,10001):
    # Since all numbers are divisible by 1,
    # we can set 1 as the initial value.
    sum_div = 1
    # The potential divisors also range from 1 to 10000,
    # therefore we can use this nested loop:
    for j in range(2, i):
        # A j value can be a divisor if the remainder is zero.
        # AND the range of the divisors must not include the number itself.
        # Since 1 is already known to be a divisor, we can start checking from 2.
        if i % j == 0 and j != i:
            # We already have 1 as the first divisor,
            # so now we have to add the other divisors.
            sum_div = sum_div + j
    # If the sum of the divisors equals the number,
    # then we got the number we need.
    if sum_div == i:
        print(i)
  • Thank you, SewentySewen! Yes, that was my bad. When I looked again at my code in the environment I deleted 1 and put 2 instead - in the j loop. But I don't know what was wrong with me when I wanted to correct this same thing over here. For some reason I corrected th wrong number 1 and wrote 20001 instead of 10001. – romanbouchouiev Jul 18 '22 at 05:15
0
  • At first glance, the reason your code is printing 1 endlessly is because you're comparing sum_div and i within the loop that iterates through j. if sum_div == i needs to be an indentation level higher than it currently is.
  • Secondly, because you've already considered 1 as a divisor when initializing sum_div, you do not need to start j from 1. It can start from 2.
  • Divisors will always be less than the number you're checking for, so you do not need j to loop from 1 to 20001 - it's enough to check till the value of i.
  • The value of i can start from 2, because we're not interested in whether 1 is a perfect number or not.

Based on these observations, here's the modified snippet that works for me.

for i in range(2, 10001):
    sum_div = 1
    for j in range(2, i):
        if i%j == 0 and j != i:
            sum_div += j
    if sum_div == i:
        print(i)
print("End of program")

Hope this helps!

  • Thank you, Akhil Unnikrishnan! Yes, that was my bad. When I looked again at my code in the environment I deleted 1 and put 2 instead - in the j loop. But I don't know what was wrong with me when I wanted to correct this same thing over here. For some reason I corrected th wrong number 1 and wrote 20001 instead of 10001. – romanbouchouiev Jul 18 '22 at 05:15