As mentioned in another post I am checking out a 100 days of code python bootcamp on Udemy and following the beginners section to see if it would be useful for my son. On day 5 loops and the range function are discussed. In one lesson Gauss pops up and the story is told how the 10 year old Gauus solved the problem of summing of the numbers 1 to 100 in 2 minutes when it was given to him by his teacher. I still remember the formula we learned in school that the sum of the first n numbers is S = (n+1)(n/2). I vaguely remember a cumbersome explanation about taking the largest (n) and smallest number (1) and summing them up to n+1 and doing that for the second largest (n-1) and smallest (2) numbers also summing up to n+1 and that you could do that n/2 times giving you the formula. This approach worked mentally well for even n but it somehow wasn’t clear what happened to the center number in case n is odd. The way it is explained in the course is a lot more straight forward and universal. Add 1 to n and also n to 1. If you put both sequences underneath eachother you get n+1 summed n times. But then you have added 1 to n 2 times, hence divide by 2.
1 + 2 + 3 + ... + n-1 + n = S n + n-1 + n-2 + ... + 2 + 1 = S + ---------------------------------- = n+1 + n+1 + n+1 + ... + n+1 + n+1 = 2S =n(n+1) = 2S Hence S = n(n+1)/2
Leave a comment