We will now see an example where the runtime of an algorithm really does make a difference. Remember the game you played with young Gauss at the beginning of lab? We will implement the game in snap.
Earlier in the lab, you made a block that returned the sum of numbers between 1 and max
. In the "Variables" palette, you will see two blocks:
The first block sums up the numbers in the numbers
list the normal, "non-Gauss" way: walking through the list and adding the numbers one by one. The second block sums up the numbers the "Gauss" way: using the formula
$$(N + 1) \times (N รท 2)$$
where, in this case, N is the same as max
. (How did we get this formula?)
add all numbers between 1 and max
block.reset timer
block after the add all numbers between 1 and max
block, since we only need to time how long the "non-Gauss" block takes to sum the numbers in the numbers
list.max
set to 10. Again, run it a few times to get an idea of the average time the computer takes to sum the numbers up.max
set to:
max
set to 10. Again, run it a few times to get an idea of the average time the computer takes to sum the numbers up.max
set to: