...

Implementing the Gaussian Challenge in Python


Carl Gauss was a German mathematician and astronomer, also known as the “Prince of Mathematics”. He is widely recognized for his contributions in the fields of science and mathematics, such as number theory, geometry, algebra, astronomy, magnetism, etc. Even today, a number of mathematical and scientific concepts are named after him. One such concept is the Gaussian Addition, which we will explore today!

It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.

– Carl Friedrich Gauss

Gaussian Addition

The Gaussian Addition Challenge is an interesting example of thinking outside the box rather than accomplishing tasks in a predetermined way.

When Carl Gauss was a child, his teacher gave him a task to add the numbers from 1 to 100. Now such a task, done one step at a time, adding the first 2 numbers, then the next, then the next, would have taken hours.

Number Addition Series (Image by Author)

But Carl Gauss came up with a quicker and smarter way to get his task done. He understood that the addition of numbers from 1 to 100 is the same as addition of 50 pairs that would sum to 101, that is, the first and the last 1 + 100 = 101, similarly the second and the second last 2 + 99 = 101, the nth and the nth last item in the series would all amount to 101, and 50 such pairs would be made. This means the total of 5050 can be easily calculated without any tedious calculations.

Addition of nth with nth last number resulting in 101 (Image by Author)

Carl Gauss was intelligent; he was able to come up with a smart technique to calculate the sum, but let’s be honest. None of us are that smart :P. While we do not have the brains of Gauss, we surely do have the advantage of programming and computers that do complex calculations for us. Let us code the above problem in Python.

Code

Let us solve the Gaussian Challenge while understanding the Python built-ins to be used:

Range

The first thing we need to understand is the Python range function. This function is used to create a sequence of numbers that can be used later in other functions, such as the for loop.

The syntax for the range function is as follows.

range = (number at which sequence starts, number at which sequence stops, step)

Suppose we have to generate a sequence of numbers from 1 to 10, with a step or difference of 1, so we will 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 Author)

Notice that we have specified ’11’ as the number at which the sequence stops. This is because, according to the syntax, the last number would be within the range, that is, in the example above, less than 11 = 10.

If we want to print the variable numbers, we won’t get a list of these numbers in the particular sequence. However, we will get a range datatype. This is because the range datatype does not store the sequence in the computer’s memory the way a list stores its items. We cannot equate the range of numbers with a list.

numbers = range(1,11)
print(numbers)
Printing the range (Image by Author)

For Loop

Next, we need to iterate through these numbers. Python loops are our go-to for any kind of iteration. In this tutorial, we will learn about the two loops and achieve the above result using both of them.

Now, since we are iterating over the range we have defined earlier, which in our case would be from 1 to 100, with the default step of 1 (we can omit mentioning that), we will use the for loop and provide it with this range. But first, we will define a variable called total that will store the sum of the sequence of numbers after every iteration. The value of total will be 0 initially, and will be increased with every iteration. So in the first iteration, when we are looping from 1 to 100, the total will be 1. In the second iteration, it will be 1 + 2 = 3. In the third iteration, it will be 3 + 3 = 6, and so on.

We will print the value total at the end. See, it amounts to 5050, the same 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 Author)

While loop

Another way to do the above task is by using Python while loop. The while loop works until a particular condition becomes false. In our case, we will have to initialize a variable i, give it the starting value of 1 and increment it by 1 only, so that it loops through the list until it reaches 101. At i = 101, the while loop’s condition will become false, and so it will stop. The value total will 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 Author)

Conclusion

In this short article, we used the range function as a quicker way to overcome our task of defining numbers from 1 to 100. We then used both the for and the while loops to solve the problem of addition, and both were able to give us the correct result.

However, as can be seen in such choices, one technique works better than the other. What do you think has been better in solving the Gaussian Challenge, the while loop or the for loop? Think in terms of complexity, time, memory used, and clarity. Clearly, one is better than the other. Do share which one you think is better than the other and why. I will look forward to your comments!

Source link

#Implementing #Gaussian #Challenge #Python