Aaron L. Feeney
Aaron L. Feeney

Reputation: 13

Adding the multiples of two ranges together gives me a different number depending how its coded

I'm learning some basics in Python 3. I tried cleaning up the code a little bit and it gave me different numbers.

At first I thought it was because the or statement was giving me the output of multiples of 3 or 5, excluding numbers like 15, 30, 45, but my or statement in the 2nd example gives me a larger number, so it's clearly not omitting any.

Goal: compute all multiples of 3, 5 that are less than 100

Attempt1

total4 = 0
for i in range(1,100):
    if i % 3 == 0: 
        total4 += i
total5 = 0
for i in range(1,100):
    if i % 5 == 0: 
        total5 += i
total6 = total4 + total5
print(total4)#1683
print(total5)#950
print(total6)#2633

Attempt2

total4 = 0
for i in range(1,100):
    if i % 3 or 1 % 5 == 0:
           total4 += i
print(total4)
#3267

Upvotes: 0

Views: 65

Answers (3)

Ralf
Ralf

Reputation: 16495

You have a little typo in your if condition in the second version. Instead of

if i % 3 or 1 % 5 == 0:

it probably should be this (note the change from 1 to i and using == 0 in both parts)

if i % 3 == 0 or i % 5 == 0:

Furthermore, I'm not sure that you are getting the output you intend. Let me ilustrate the situation:

divisible_by_3_count = 0
divisible_by_3_sum = 0
divisible_by_5_count = 0
divisible_by_5_sum = 0
divisible_by_3_or_5_count = 0
divisible_by_3_or_5_sum = 0
divisible_by_3_and_5_count = 0
divisible_by_3_and_5_sum = 0

for i in range(1, 100):             # will not include the number 100
    if i % 3 == 0:
        divisible_by_3_count += 1
        divisible_by_3_sum += i
    if i % 5 == 0:
        divisible_by_5_count += 1
        divisible_by_5_sum += i
    if i % 3 == 0 or i % 5 == 0:    # uses OR
        divisible_by_3_or_5_count += 1
        divisible_by_3_or_5_sum += i
    if i % 3 == 0 and i % 5 == 0:   # uses AND
        divisible_by_3_and_5_count += 1
        divisible_by_3_and_5_sum += i

print(divisible_by_3_count)                          #   33
print(divisible_by_3_sum)                            # 1683
print(divisible_by_5_count)                          #   19
print(divisible_by_5_sum)                            #  950
print(divisible_by_3_or_5_count)                     #   46
print(divisible_by_3_or_5_sum)                       # 2318
print(divisible_by_3_and_5_count)                    #    6
print(divisible_by_3_and_5_sum)                      #  315

print(divisible_by_3_count + divisible_by_5_count)   #   52
print(divisible_by_3_sum + divisible_by_5_sum)       # 2633

The last option (the sum of 2 variables) might not really be accurate as some numbers are summed twice; for example the number 30 is already summed in divisible_by_3_sum and again in divisible_by_5_sum, that is why:

# 33 + 19 > 46
>>> (divisible_by_3_count + divisible_by_5_count) > divisible_by_3_or_5_count
True

# 1683 + 950 > 2318
>>> (divisible_by_3_sum + divisible_by_5_sum) > divisible_by_3_or_5_sum
True

I hope this helps you.

Upvotes: 1

Ignatius
Ignatius

Reputation: 3292

Your second attempt doesn't do what you intended, for two reasons.

First, the typo in 1%5 == 0. I suppose you intended i instead of 1. However, correcting that will give total4==3582, which is still not what you expected.

This is because you wrote the if-statement not as you intended. i%3 or i%5 == 0 means "True if at least one of i%3 or i%5==0 is true". (Note in computer science or nearly always mean "at least one of them is true.") To understand what "i%3 is true", you should know that all integers other than 0 is considered "true".

So the correct condition for your if-statement is i%3==0 or i%5==0.

Well, that still gives a different value of 2318. This is practically (the sum of multiples of 3 below 100) + (the sum of multiples of 5 below 100) - (the sum of multiples of 15 below 100). (Inclusion-exclusion principle!)

Your first code ('Attempt1') doesn't calculate the sum of multiples of 15 below 100, only calculates (the sum of multiples of 3 below 100) + (the sum of multiples of 5 below 100), so if you want to produce a same result as your second code ('Attempt2'), you will need another variable to accumulate the sum of multiples of 15.

Upvotes: 2

sroca
sroca

Reputation: 114

Your second attempt condition should be something like:

    if (i % 3 == 0) or (i % 5 == 0):

That is, the ==0 condition shall be applied twice, and I bet 1 % 5 in your code was a typo.

You may want to print(i) inside the loop to debug which numbers are being counted.

Upvotes: 1

Related Questions