up
This commit is contained in:
parent
5372c18379
commit
3defacc09d
314 changed files with 1761 additions and 1704 deletions
40
1-js/8-more-functions/01-recursion/01-sum-to/solution.md
Normal file
40
1-js/8-more-functions/01-recursion/01-sum-to/solution.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
The solution using a loop:
|
||||
|
||||
```js run
|
||||
function sumTo(n) {
|
||||
let sum = 0;
|
||||
for (let i = 1; i <= n; i++) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
alert( sumTo(100) );
|
||||
```
|
||||
|
||||
The solution using recursion:
|
||||
|
||||
```js run
|
||||
function sumTo(n) {
|
||||
if (n == 1) return 1;
|
||||
return n + sumTo(n - 1);
|
||||
}
|
||||
|
||||
alert( sumTo(100) );
|
||||
```
|
||||
|
||||
The solution using the formula: `sumTo(n) = n*(n+1)/2`:
|
||||
|
||||
```js run
|
||||
function sumTo(n) {
|
||||
return n * (n + 1) / 2;
|
||||
}
|
||||
|
||||
alert( sumTo(100) );
|
||||
```
|
||||
|
||||
P.S. Naturally, the formula is the fastest solution. It uses only 3 operations for any number `n`. The math helps!
|
||||
|
||||
The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it's slower.
|
||||
|
||||
P.P.S. The standard describes a "tail call" optimization: if the recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution and we don't need to remember its execution context. In that case `sumTo(100000)` is countable. But if your Javascript engine does not support it, there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
|
36
1-js/8-more-functions/01-recursion/01-sum-to/task.md
Normal file
36
1-js/8-more-functions/01-recursion/01-sum-to/task.md
Normal file
|
@ -0,0 +1,36 @@
|
|||
importance: 5
|
||||
|
||||
---
|
||||
|
||||
# Sum all numbers till the given one
|
||||
|
||||
Write a function `sumTo(n)` that calculates the sum of numbers `1 + 2 + ... + n`.
|
||||
|
||||
For instance:
|
||||
|
||||
```js no-beautify
|
||||
sumTo(1) = 1
|
||||
sumTo(2) = 2 + 1 = 3
|
||||
sumTo(3) = 3 + 2 + 1 = 6
|
||||
sumTo(4) = 4 + 3 + 2 + 1 = 10
|
||||
...
|
||||
sumTo(100) = 100 + 99 + ... + 2 + 1 = 5050
|
||||
```
|
||||
|
||||
Make 3 solution variants:
|
||||
|
||||
1. Using a for loop.
|
||||
2. Using a recursion, cause `sumTo(n) = n + sumTo(n-1)` for `n > 1`.
|
||||
3. Using the [arithmetic progression](https://en.wikipedia.org/wiki/Arithmetic_progression) formula.
|
||||
|
||||
An example of the result:
|
||||
|
||||
```js
|
||||
function sumTo(n) { /*... your code ... */ }
|
||||
|
||||
alert( sumTo(100) ); // 5050
|
||||
```
|
||||
|
||||
P.S. Which solution variant is the fastest? The slowest? Why?
|
||||
|
||||
P.P.S. Can we use recursion to count `sumTo(100000)`?
|
Loading…
Add table
Add a link
Reference in a new issue