Massimo
Massimo

Reputation: 7

Adding multiples of 3 and 5 less than 1000

I am trying to find the sum of the multiples of 3 and 5 while the sum is less than 1000. When I run the code, I just get a never ending list of 0s and it says 'Too much output to process'. I don't know where I am going wrong and would appreciate feed back.

result = 0
while result <= 1000:
    i = 0
    if i % 3 == 0 or i % 5 == 0:
        print i
        result += i
else:
    i += 1

EDIT

The loop resets i to 0 everytime. This means that it is always less than 1000.

Upvotes: 0

Views: 124

Answers (5)

Fernando Coelho
Fernando Coelho

Reputation: 257

In case anyone is wondering, this is a classic problem, I know it from Euler project: https://projecteuler.net/problem=1

Besides all the answers given here, there is an O(1) answer using Arithmetic Progression (https://en.wikipedia.org/wiki/Arithmetic_progression).

Basically we need to calculate the AP of 3 to the maximum divisor of 3 that is less than a 1000 add the AP of 5 to the maximum divisor of 5 that is less than a 1000 minus AP of 15 to the maximum divisor of 15 that is less than a 1000 (this step removes itens that were added twice because they are multiples of 3 and 15).

Here is the code:

def ap(i, range_start, range_end):
    a1 = range_start
    an = range_end
    n = (an - a1)/i + 1

    return (a1 + an) * n / 2

def ap_with_limit(i, limit):
    limit -= 1
    return ap(i, i, limit - (limit%i))

print ap_with_limit(3, 1000) + ap_with_limit(5, 1000) - ap_with_limit(15, 1000)
# 233168

Upvotes: 0

Milan Malla
Milan Malla

Reputation: 21

def sum(): 
    result = 0
    i = 0
    # Put i=0 before loop begin
    while result <= 1000:
        if i % 3 == 0 or i % 5 == 0:
            print i
            result += i
            i+=1  # Put i+=1 after result here
        else:
            i += 1
    print ("sum= "+str(result))
sum()

Just put i=0 before the loop begins and i+=1 after result

Upvotes: 0

Eric Duminil
Eric Duminil

Reputation: 54313

You had an infinite loop because i wasn't incremented, and result either.

It then boils down to what you want exactly :

sum less than 1000

result = 0
i = 0 
while result <= 1000:
    i += 1
    if i % 3 == 0 or i % 5 == 0:
        result += i

print(result - i)
# 998

elements less than 1000

If you're refering to the Euler problem, then the sum shouldn't be less than 1000, but the elements :

total_sum = 0
for i in range(1000):
    if (i % 3 == 0 or i % 5 == 0):
        total_sum = total_sum + i
print total_sum  
# 233168

An alternative would be :

sum(set(range(0,1000,3)) | set(range(0,1000,5)))
# 233168

or :

sum(range(0,1000,3)) + sum(range(0,1000,5)) - sum(range(0,1000,15))
# 233168

Upvotes: 2

UncleZeiv
UncleZeiv

Reputation: 18488

A fun exercise is to rewrite this in a more "pythonic" manner, for instance:

sum(filter(lambda x: x % 3 == 0 or x % 5 == 0, xrange(1000)))

or even better:

sum(x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0)

which both give 233168 as a result.

(Please note that in both cases sum() is a Python builtin function).

By the way, in your problem statement, you mention "less than 1000", but your code includes 1000 in the loop.

Upvotes: 1

Kewl
Kewl

Reputation: 3417

You never increment i. Put i = 0 outside of the loop, and i += 1 inside of it. i.e.

result = 0
i = 0

while result <= 1000:
    if i % 3 == 0 or i % 5 == 0:
        print(i)
        result += i
    i += 1

Upvotes: 0

Related Questions