In this tutorial, we will write step-by-step a simple algorithm that will compute the sum of all numbers from 1 to a user-inputted number.
When you open Turing, you should see a blank window like in the screenshot above. Otherwise, click on New.
Before writing an algorithm, you must first think about how it will work ; which values it will take in input, and what it will return in output. A pencil and a sheet of paper are recommended, to make a quick draft of the global structure of the algorithm, although it’s not necessary for small algorithms such as the one we’ll write today.
Let’s have a look at the wording: “a simple algorithm that will compute the sum of all numbers from 1 to a user-inputted number”.
It is true that formulas exist to compute such sums (e.g. Gauss’ formula for integer summations), but here we will do it in a simpler and more easily understandable way.
We will use here what we call variables. Although that name is also used in maths, these are two fundamentally different things. In maths, a variable is a thing, whose value can’t change (but doesn’t need to be known nor unique), that contains a number, and that can/must verify constraints (that are called equations). In programming, a variable can be assimilated to a box in which we can put anything we want at any moment.
To recap: a variable is a thing in which we can put whatever we want and that we can modify whenever we want.
First, we’ll ask the user for a number ; which number shall we use to compute the sum. We’ll use for that the Read user INPUT instruction. Click on the corresponding button:
A window will then open:
The Read user INPUT instruction has two parameters:
- the variable in which we will put the value inputted by the user
- the message to display so that the user knows what we the value is for
The message is optional ; if it’s not specified, a generic message containing the variable name will be displayed instead.
The Variable field shall contain the name of the variable. A variable name can contain letters and digits (but can’t start with a digit) and can’t contain spaces. Here, we’ll use “x”.
Next, click on OK.
Your algorithm should look like this:
We’ll try it to make sure everything works. Click on Run.
You should see this in the Output window:
As you can see, the generic message has been displayed, and the text field has been enabled. Type a number, for example 5, and either press Enter or click on the little green button.
The value showed up, and the program stopped. Which is quite logical given that the program only contains one instructin.
We can move on to the next step, which will consist of computing the sum of the numbers from 1 up to x. We can think about it differently: for example, we’ll say that we initialize the “counter” to 0, and then add 1, then 2, then 3 and so on until we end by adding x to the “counter”.
That counter would pretty much be a box in which we put a number that we can modify whenever we want. Reminds you of something?
As you may have guessed, we’ll use a variable for that.
Click on the DEFINE variable button.
The instruction has two settings:
- the variable that we want to define
- the value to put in the variable
Here, we want to initialize the counter to zero. Click on OK. The algorithm should look like this:
Now that the counter is at zero, we must add 1, then 2, then 3 and so on up to x. Put step by step, it would pretty much look like this:
- add 1 to the counter
- add 2 to the counter
- add 3 to the counter
- add x to the counter
That’s repetitive. That’s very repetitive. That’s too repetitive.
You may have heard that mathematicians are lazy. That’s true. You may also have heard that computers were invented by mathematicians. Thus, we can conclude that developers are lazy. Lazy people don’t like repetitive things, because it’s repetitive. It’s always the same thing. It’s not interesting. For that reason, we invented a cool thing called loops. A loop is something that allows you to do the same thing multiple times.
There are different kinds of loops in programming. In algorithms, only two will be used:
- the FOR variable FROM begin TO end loop : executes the instructions for all values of variable between begin and end (adding 1 each time)
- the WHILE condition loop : executes the instructions while condition is true
You’ve guessed it, the one we’ll use here is the FOR loop. We’ll add one. Click on the Blocks tab and on the button FOR loop.
Here, current will start at 1 and end at x.
Click on OK. Your algorithm should look like this:
Next, we want that at each “turn”, it adds the value to the counter. So, at each turn, it will modify the variable. Select the line of the loop and click on DEFINE variable.
It means that for each value of current, we add current to counter. In other words, for each value of current, counter takes the value counter + current.
The algorithm should look like this:
That’s nice and all, but what do we do about counter?
We need to display it at the end of the program, otherwise it will just compute its value and forget about it. Click on DISPLAY value.
Warning: depending on how you do it, the line may be added like this:
That is not what we want, since that way it would display the value at each iteration of the loop (instead of just one time at the end). Select the line and click on the arrow in the upper right corner:
The line should get to the right location and the final algorithm should look like this:
We can now test it. Click on Run and try inputting some values. For example, the number 100 should give the output 5050.