maku - 6 months ago 68

Python Question

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):`

print(x+x)

Any help is much appreciated!

Answer

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.