Carl Gauss was a German mathematician and astronomer, also generally known as the “Prince of Mathematics”. He’s well known for his contributions within the fields of science and arithmetic, comparable to number theory, geometry, algebra, astronomy, magnetism, etc. Even today, various mathematical and scientific concepts are named after him. One such concept is the Gaussian Addition, which we’ll explore today!
It will not be knowledge, however the act of learning, not possession however the act of getting there, which grants the best enjoyment.
– Carl Friedrich Gauss
Gaussian Addition
The Gaussian Addition Challenge is an interesting example of pondering outside the box relatively than accomplishing tasks in a predetermined way.
When Carl Gauss was a toddler, his teacher gave him a task so as to add the numbers from 1 to 100. Now such a task, done one step at a time, adding the primary 2 numbers, then the following, then the following, would have taken hours.
Number Addition Series (Image by Writer)
But Carl Gauss got here up with a quicker and smarter option to get his task done. He understood that the addition of numbers from 1 to 100 is similar as addition of fifty pairs that may sum to 101, that’s, the primary and the last 1 + 100 = 101, similarly the second and the second last 2 + 99 = 101, the nth and the nth last item within the series would all amount to 101, and 50 such pairs could be made. This implies the whole of 5050 may be easily calculated with none tedious calculations.
Addition of nth with nth last number leading to 101 (Image by Writer)
Carl Gauss was intelligent; he was capable of provide you with a wise technique to calculate the sum, but let’s be honest. None of us are that smart :P. While we do not need the brains of Gauss, we surely do have the advantage of programming and computers that do complex calculations for us. Allow us to code the above problem in Python.
Code
Allow us to solve the Gaussian Challenge while understanding the Python built-ins for use:
Range
The very first thing we’d like to know is the Python range function. This function is used to create a sequence of numbers that may be used later in other functions, comparable to the for loop.
The syntax for the range function is as follows.
Suppose we’ve to generate a sequence of numbers from 1 to 10, with a step or difference of 1, so we’ll use this range function as follows:
numbers = range(1,11)
for i in numbers:
print(i)
Printing the numbers using the range function (Image by Writer)
Notice that we’ve specified ’11’ because the number at which the sequence stops. It is because, in accordance with the syntax, the last number could be throughout the range, that’s, in the instance above, lower than 11 = 10.
If we wish to print the variable numbers, we won’t get a listing of those numbers in the actual sequence. Nevertheless, we’ll get a variety datatype. It is because the range datatype doesn’t store the sequence in the pc’s memory the way in which a listing stores its items. We cannot equate the range of numbers with a listing.
numbers = range(1,11)
print(numbers)
Printing the range (Image by Writer)
For Loop
Next, we’d like to iterate through these numbers. Python loops are our go-to for any sort of iteration. On this tutorial, we’ll learn concerning the two loops and achieve the above result using each of them.
Now, since we’re iterating over the range we’ve defined earlier, which in our case could be from 1 to 100, with the default step of 1 (we will omit mentioning that), we’ll use the for loop and supply it with this range. But first, we’ll define a variable called total that may store the sum of the sequence of numbers after every iteration. The worth of total might be 0 initially, and might be increased with every iteration. So in the primary iteration, after we are looping from 1 to 100, the whole might be 1. Within the second iteration, it is going to be 1 + 2 = 3. Within the third iteration, it is going to be 3 + 3 = 6, and so forth.
We are going to print the worth total at the top. See, it amounts to 5050, the identical value as Gauss.
numbers = range(1,101)
total = 0
for i in numbers:
total = total + i
print("Total: ", total)
Totak using For Loop (Image by Writer)
While loop
One other option to do the above task is by utilizing Python while loop. The while loop works until a specific condition becomes false. In our case, we may have to initialize a variable i, give it the starting value of 1 and increment it by 1 only, in order that it loops through the list until it reaches 101. At i = 101, the while loop’s condition will develop into false, and so it is going to stop. The worth total might be printed.
numbers = range(1,101)
total = 0
i = 1
while i in numbers:
total = total + i
i = i + 1
print("Total: ", total)
Output with While loop (Image by Writer)
Conclusion
On this short article, we used the range function as a quicker option to overcome our task of defining numbers from 1 to 100. We then used each the for and the while loops to resolve the issue of addition, and each were able to provide us the proper result.
Nevertheless, as may be seen in such selections, one technique works higher than the opposite. What do you think that has been higher in solving the Gaussian Challenge, the while loop or the for loop? Think when it comes to complexity, time, memory used, and clarity. Clearly, one is best than the opposite. Do share which one you think that is best than the opposite and why. I’ll sit up for your comments!