## Quick tutorial for beginners 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8983306,"math_prob":0.93760645,"size":5617,"snap":"2019-35-2019-39","text_gpt3_token_len":1272,"char_repetition_ratio":0.13326207,"word_repetition_ratio":0.05848514,"special_character_ratio":0.22734556,"punctuation_ratio":0.11730449,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9773934,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34],"im_url_duplicate_count":[null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null,8,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-26T01:36:34Z\",\"WARC-Record-ID\":\"<urn:uuid:f23f2adb-9d03-42cf-b282-78ec3420a0a8>\",\"Content-Length\":\"25720\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5de7d655-c2d1-43bb-967d-a5fb20589e86>\",\"WARC-Concurrent-To\":\"<urn:uuid:82d52db2-fdda-4130-aa1e-c5115ea69363>\",\"WARC-IP-Address\":\"104.18.36.32\",\"WARC-Target-URI\":\"https://turingapp.ml/category/tutorials/\",\"WARC-Payload-Digest\":\"sha1:WS7TFLNIXIDZTC5ERVCSNVOXATQG4XX4\",\"WARC-Block-Digest\":\"sha1:UJ7KRRNZ2Z6L5U4O6V7UGFTI4CL4DTA4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027330913.72_warc_CC-MAIN-20190826000512-20190826022512-00009.warc.gz\"}"}