Alex wants to create an algorithm to calculate the sum of the first 100 positive integers.
He tells the algorithm to
1. Create the variable
sum
with an initial value of 0.
2. Start with the value of
3. Add
to the sum
4. Add 1 to
5. Continue doing so till
Does this algorithm work?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Alex's algorithm will only sum the first 9 9 positive integers. The part where i is added to
sum
is located before the part where 1 is added to i , so the loop ends before it gets a chance to add 1 0 0 tosum
.And of course, here is some Python:
Python 3.4: