Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Use a loop to add the numbers from to . (The mathematician Gauss did it as a child, with a clever idea.)
Start the total before the loop, and add to it inside the loop.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Start total = 0 before the loop. Add i in every round, with range(1, 101).
Result. The loop shows .
Check with Gauss's idea. Pair the first and the last: , , and so on. There are pairs, so the sum is . ✓
Log in to join the discussion.
Loading…