maku maku - 1 year ago 103
Python Question

Sum up all the integers in range()

I need to write a program that sums up all the integers which can be divided by 3 in the range of 100 to 2000. I'm not even sure where to start, so far I've got this tiny piece of code written which isn't correct.

for x in range(100, 2001, 3):

Any help is much appreciated!

aga aga
Answer Source

Use generator expression and sum function here:

res = sum(x for x in range(100, 2001) if x % 3 == 0)

It's pretty self-explanatory code: you're summing all the numbers from 100 to 2000, inclusive, which are divisible by three.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download