forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove live code & console.log, leave examples as comments (ProjectEu…
…ler, Recursive).
- Loading branch information
Showing
17 changed files
with
48 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,13 +1,15 @@ | ||
// https://en.wikipedia.org/wiki/Fibonacci_number | ||
|
||
const fibonacci = (N) => { | ||
if (N === 0 || N === 1) return N | ||
// https://en.wikipedia.org/wiki/Fibonacci_number | ||
|
||
/** | ||
* Return the N-th Fibonacci number | ||
* | ||
* @param {number} N | ||
* @returns {number} | ||
*/ | ||
export const fibonacci = (N) => { | ||
if (N === 0 || N === 1) { | ||
return N | ||
} | ||
return fibonacci(N - 2) + fibonacci(N - 1) | ||
} | ||
|
||
// testing | ||
(() => { | ||
const number = 5 | ||
console.log(number + 'th Fibonacci number is ' + fibonacci(number)) | ||
})() |
This file contains 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
This file contains 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
This file contains 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,16 +1,18 @@ | ||
// wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi | ||
// Recursive Javascript function to solve tower of hanoi | ||
|
||
function TowerOfHanoi (n, fromRod, toRod, auxRod) { | ||
export function TowerOfHanoi (n, from, to, aux, output = []) { | ||
if (n === 1) { | ||
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`) | ||
return | ||
output.push(`Move disk 1 from rod ${from} to rod ${to}`) | ||
return output | ||
} | ||
TowerOfHanoi(n - 1, fromRod, auxRod, toRod) | ||
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`) | ||
TowerOfHanoi(n - 1, auxRod, toRod, fromRod) | ||
TowerOfHanoi(n - 1, from, aux, to, output) | ||
output.push(`Move disk ${n} from rod ${from} to rod ${to}`) | ||
TowerOfHanoi(n - 1, aux, to, from, output) | ||
return output | ||
} | ||
// Driver code | ||
const n = 4 | ||
TowerOfHanoi(n, 'A', 'C', 'B') | ||
// A, C, B are the name of rods | ||
|
||
// Driver code (A, C, B are the name of rods) | ||
|
||
// const n = 4 | ||
// TowerOfHanoi(n, 'A', 'C', 'B') |
This file contains 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