Skip to content

Instantly share code, notes, and snippets.

@marcuszierke
Last active October 25, 2021 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcuszierke/efb44599be507d11776aef8938f93ad9 to your computer and use it in GitHub Desktop.
Save marcuszierke/efb44599be507d11776aef8938f93ad9 to your computer and use it in GitHub Desktop.
Tail Recursion - Collartz
const simpleFac = (n) => {
if (n === 1) return n
return n * simpleFac(n - 1)
}
const TRFac = (n, a) => {
if (n === 0) return a
return TRFac(n - 1, a * n)
}
// hier wird der Stack 5 mal gecallt => was Sinn macht
console.log(simpleFac(5))
// hier wird der Stack nur ein einziges Mal gecallt
// => was es ja auch tun soll - TR - aber sich mir nicht erschliesst
console.log(TRFac(5,1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment