Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Recursion and stack#163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
32 changes: 16 additions & 16 deletions1-js/06-advanced-functions/01-recursion/01-sum-to/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
Řešení pomocí cyklu: | ||
```js run | ||
functionsečtiDo(n) { | ||
letsoučet = 0; | ||
for (let i = 1; i <= n; i++) { | ||
součet += i; | ||
} | ||
returnsoučet; | ||
} | ||
alert(sečtiDo(100) ); | ||
``` | ||
Řešení pomocí rekurze: | ||
```js run | ||
functionsečtiDo(n) { | ||
if (n == 1) return 1; | ||
return n +sečtiDo(n - 1); | ||
} | ||
alert(sečtiDo(100) ); | ||
``` | ||
Řešení pomocí vzorce: `sečtiDo(n) = n*(n+1)/2`: | ||
```js run | ||
functionsečtiDo(n) { | ||
return n * (n + 1) / 2; | ||
} | ||
alert(sečtiDo(100) ); | ||
``` | ||
P.S.Nejrychlejší řešení je pochopitelně pomocí vzorce. Pro jakékoli číslo `n` vykonává pouze 3 operace. Matematika pomáhá! | ||
Druhá nejlepší co do rychlosti je varianta s cyklem. V rekurzívní i v cyklové variantě sčítáme stejná čísla, ale rekurze vyžaduje vnořená volání a správu prováděcího zásobníku. To vyžaduje další zdroje, takže je pomalejší. | ||
P.P.S.Některé motory podporují optimalizaci „koncového volání“: je-li rekurzívní volání ve funkci úplně poslední a žádné další výpočty se neprovádějí, pak se nemusí obnovovat provádění vnější funkce, takže si motor nemusí pamatovat její prováděcí kontext. Tím se sníží paměťová zátěž. Pokud však motor JavaScriptu nepodporuje optimalizaci koncového volání (a většina motorů ji nepodporuje),nastane chyba: bude překročena maximální velikost zásobníku, protože celková velikost zásobníku je obvykle omezena. |
34 changes: 17 additions & 17 deletions1-js/06-advanced-functions/01-recursion/01-sum-to/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions1-js/06-advanced-functions/01-recursion/02-factorial/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
Podle definice můžeme faktoriál `n!`zapsat jako `n * (n-1)!`. | ||
Jinými slovy, výsledek funkce `faktoriál(n)`můžeme vypočítat jako`n`vynásobené výsledkem volání `faktoriál(n-1)`.A volání pro`n-1`může rekurzívně klesat níž a níž až k `1`. | ||
```js run | ||
functionfaktoriál(n) { | ||
return (n != 1) ? n *faktoriál(n - 1) : 1; | ||
} | ||
alert(faktoriál(5) ); // 120 | ||
``` | ||
Základem rekurze je hodnota`1`.Zde můžeme jako základ vzít také`0`, na tom příliš nezáleží, ale to nám dá jeden rekurzívní krok navíc: | ||
```js run | ||
functionfaktoriál(n) { | ||
return n ? n *faktoriál(n - 1) : 1; | ||
} | ||
alert(faktoriál(5) ); // 120 | ||
``` |
14 changes: 7 additions & 7 deletions1-js/06-advanced-functions/01-recursion/02-factorial/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
62 changes: 31 additions & 31 deletions1-js/06-advanced-functions/01-recursion/03-fibonacci-numbers/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 9 additions & 8 deletions1-js/06-advanced-functions/01-recursion/03-fibonacci-numbers/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.