3

I try to make a program that print the factors of a number and print its abundant factors. But when im working with it, i found some problem. I don't know why the output is different if i declare a variable named "sum_abundant_factor" inside the for loop and outside the for loop.

"sum_abundant_factor" is a variable that i use to check whether the factor is abundant or not. (abundant number is a number that is smaller than the sum of its proper divisors).

This is my code and output when i declare "sum_abundant_factor" inside the for loop :

input_number = int(input('Input number : '))
factor = ''
sum_factor = 0
abundant_factor = ''

for i in range(1, input_number+1):
    if input_number % i == 0:
        sum_abundant_factor = 0
        factor += str(i) + ' '
        if i < input_number :
           sum_factor += i
        for j in range(1, i):
           if i % j == 0:
              sum_abundant_factor += j
        if sum_abundant_factor > i:
           abundant_factor += str(i) + ' '

print('Factors of {} :'.format(input_number), factor)
print('Abundant Factors :', abundant_factor)

Output :
Input number : 54
Factors of 54 : 1 2 3 6 9 18 27 54
Abundant Factors : 18 54

And this is my code and output when i declare "sum_abundant_factor" before (outside) the for loop :

input_number = int(input('Input number : '))
factor = ''
sum_factor = 0
abundant_factor = ''
sum_abundant_factor = 0

for i in range(1, input_number+1):
    if input_number % i == 0:
        factor += str(i) + ' '
        if i < input_number:
           sum_factor += i
        for j in range(1, i):
           if i % j == 0:
              sum_abundant_factor += j
       if sum_abundant_factor > i:
           abundant_factor += str(i) + ' '

print('Factors of {} :'.format(input_number), factor)
print('Abundant Factors :', abundant_factor)

Output :
Input number : 54
Factors of 54 : 1 2 3 6 9 18 27 54
Abundant Factors : 6 9 18 27 54

I have no idea why the abundant factors output is different when i declaring the variable inside and outside for loop. Can anyone help me explain it to me ?

2
  • 6
    For each loop, sum_abundant_factor will be set to 0 initially. With being declared outside of the loop, it will keep its value, and won't be 0 initially. You keep adding to its value, but not reset before each run. Commented Jan 3, 2021 at 9:27
  • Oh i get it, thanks for explaining. Commented Jan 3, 2021 at 9:30

2 Answers 2

1

If the sum_abundant_factor = 0 is declared inside the loop, every time it resets the sum_abundant_factor to 0 when it is the next element of the range.

Here is an easier example:

a = [1, 2, 3]
for i in a:
    b = 0
    b += i
    print(b)

Every time it is the next element in the a list, it will reset b to 0, so then 0 added to i would change nothing to the iterator i, so the above code would output:

1
2
3

Whereas if you define the b variable outside the loop:

a = [1, 2, 3]
b = 0
for i in a:
    b += i
    print(b)

It will output:

1
3
6

Because it won't reset, it will keep adding, so 0 + 1 is 1 (the first printed value), and 1 + 2 is 3 (the second printed value) and 3 + 3 is 6 (the third printed value).

Sign up to request clarification or add additional context in comments.

Comments

1

If you put the sum_abundunt_factor inside the for loop, every time when you loop through it, it will be set to 0 if the if input_number % i == 0: happens, even though you have code such as sum_abundant_factor += j. It outputs that number, but then resets to 0 the next time you loop through it when the if statement happens. If you leave it outside, the number will only be added every time sum_abundant_factor += j occurs. The number won't be reset to 0 as it is outside the for loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.