Solutions for Simple Adding

View the challenge

Official solution

This challenge requires you to add up all the numbers from 1 to a given argument. For example, if the parameter num is 4, your program should add up 1 + 2 + 3 + 4 and return 10. This will be pretty simple to write out as a loop. We'll maintain a variable and keep adding to it as we loop from 1 to num.

```
function SimpleAdding(num) {
var answer = 0;
// loop from 1 to num
for (var i = 1; i <= num; i++) {
answer = answer + i;
}
return answer;
}
SimpleAdding(4);
```

```
def SimpleAdding(num):
answer = 0
# loop from 1 to num
for i in range(1, num+1):
answer = answer + i
return answer
print SimpleAdding(4)
```

```
def SimpleAdding(num)
answer = 0
# loop from 1 to num
(1..num).each do |i|
answer += i
end
return answer
end
puts SimpleAdding(STDIN.gets)
```

There is also a very clever way to calculate the sum of the numbers **1 + 2 + 3 + ...**. The sum of the first N natural numbers is equal to **n(n+1)/2** (see proof and why this is so).

```
function SimpleAdding(num) {
return (num*(num+1))/2;
}
SimpleAdding(4);
```

```
def SimpleAdding(num):
return (num*(num+1))/2
print SimpleAdding(4)
```

```
def SimpleAdding(num)
return (num*(num+1))/2
end
puts SimpleAdding(STDIN.gets)
```

Top Rated User Solution

`loading...`

`loading...`

`loading...`

`loading...`

`loading...`

`loading...`

`loading...`

`loading...`

`loading...`

`loading...`

Top User Solutions

Loading...