## Introduction

In this article we are going to analyze an algorithm to **add the numbers from 1 to 10**, that is to say we will create a function that solves the operation **1+2+3+…+10**, this function will be programmed in a **generic **way to **calculate the sum of all the natural numbers from 1 to N**, **being N any natural number**, for example if N is 100, our algorithm will add the natural numbers from 1 to 100.

## How can this situation be solved?

One thing we could do is to explicitly add up all those numbers, as shown below:

**sum = 1+2+3+4+5+6+7+8+9+10**

Here the variable sum is assigned the sum of all natural numbers up to 10.

However this way of doing it is **very rigid**, it **only allows us to solve that particular situation**, if we would like to add the numbers up to 15, we have to go to the code and add the numbers manually and this of course is very impractical and cannot be modified at **run time**. Besides, why go to the trouble of writing the sum when you could directly assign the value 55 to the variable sum, right?

So we are going to solve this problem with a more flexible strategy, we are going to do this operation in several steps using mathematical properties such as the **associative **or the **existence of the neutral element for the sum**. This will allow us to have an algorithm that not only adds the integers from 1 to 10 but from 1 to any number we want.

## Algorithm for adding N natural numbers

We can** think this big sum in smaller sums**, for example if we start from 0, first we solve 0+1 and store this result in a variable, then in another step, to the result of 0+1 we add 2, in another step, to that result we add 3 and so on until we reach the last value that we want to add. The number of steps to be taken goes from the first element to be added to the last element, that is, in the example, from Step 1 to Step 10.

This can be solved by using a variable that works as an **accumulator of the sum** and a **repetitive loop** that adds the numbers to the accumulator in each iteration. So for the variables we declare:

**public int sum;public int n=10;**

**The accumulator is initialized to 0**, because 0 is the neutral element for the sum, 0 added to any number is equal to that number. We do this with the following instruction:

**sum=0;**

Then we define a for loop, if you are using Visual Studio something you can do is type “for” and press the Tab key twice, that will autocomplete the syntax of the **for **loop.

We are going to initialize the loop in the value 1 and make it end in the variable N, in this particular case it is important that we use the sign “**less or equal**“. Summarizing what we are going to obtain is a first iteration in which “i” is 1 and in the last iteration i is 10 (in this particular case).

**for(int i=1;i<=n;i++){ sum=sum+i;}**

Inside the loop we will execute the instruction that is observed in the code fragment from above, variable * sum *equals the variable

*plus*

**sum***, this means that first we will calculate this operation and then we will assign it to this variable, in the first iteration,*

**i***will be worth 0, to that we add*

**sum***that is worth 1, 0+1 equals 1 and that we assign it in the variable*

**i***, in the following iteration*

**sum***is worth 1 and*

**sum***is worth 2, so that the operation 1+2 is solved and it is assigned in the variable*

**i***. So on and so forth until the last number is reached.*

**sum**## Algorithm test run in Unity

The complete code in C# language would look approximately as follows:

**public int sum;public int n=10;**

void Start(){

sum=0;

for(int i=1;i<=n;i++){

sum=sum+i;

}

}

void Start(){

sum=0;

for(int i=1;i<=n;i++){

sum=sum+i;

}

}

We proceed to test the code, save the changes in the Script and execute it, in our case we do this by assigning the Script that contains the code to any GameObject of the hierarchy of our Unity Project and entering the game mode. When doing this, Unity automatically executes the Start function of that Script and the result of the sum is stored in the variable sum.

As the variable “** n**” is public, you can modify its value in the inspector, for example set it to 100 and solve for the sum of the natural numbers from 1 to 100.