Last active
May 7, 2021 01:11
-
-
Save robertsosinski/62abbac4eacda6548be1 to your computer and use it in GitHub Desktop.
Recursive Factorial and Fibonacci in Different Languages
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
def fac(n: Int): BigInt = n match { | |
case 0 => 1 | |
case _ => n * fac(n - 1) | |
} | |
def facTail(n: Int, acc: BigInt = 1): BigInt = n match { | |
case 0 => acc | |
case _ => facTail(n - 1, n * acc) | |
} | |
def fib(n: Int): Int = n match { | |
case 0 => 0 | |
case 1 => 1 | |
case _ => fib(n - 1) + fib(n - 2) | |
} | |
def fibTail(n: Int, a: Int = 1, b: Int = 0): Int = n match { | |
case 0 => 0 | |
case 1 => a + b | |
case _ => fibTail(n - 1, b, a + 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
-module(recursion). | |
-export([fac/1, fac_tail/1, fib/1, fib_tail/1]). | |
fac(0) -> 1; | |
fac(N) when N > 0 -> N * fac(N - 1). | |
fac_tail(N) -> fac_tail(N, 1). | |
fac_tail(0, Acc) -> Acc; | |
fac_tail(N, Acc) when N > 0 -> fac_tail(N - 1, N * Acc). | |
fib(0) -> 0; | |
fib(1) -> 1; | |
fib(N) when N > 1 -> fib(N - 1) + fib(N - 2). | |
fib_tail(N) -> fib_tail(N, 1, 0). | |
fib_tail(0, _, _) -> 0; | |
fib_tail(1, A, B) -> A + B; | |
fib_tail(N, A, B) when N > 1 -> fib_tail(N - 1, B, A + 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
defmodule Recursion do | |
def fac(0) do | |
1 | |
end | |
def fac(n) when n > 0 do | |
n * fac(n - 1) | |
end | |
def fac_tail(n) do | |
fac_tail(n, 1) | |
end | |
defp fac_tail(0, acc) do | |
acc | |
end | |
defp fac_tail(n, acc) when n > 0 do | |
fac_tail(n - 1, n * acc) | |
end | |
def fib(0) do | |
0 | |
end | |
def fib(1) do | |
1 | |
end | |
def fib(n) when n > 1 do | |
fib(n - 1) + fib(n - 2) | |
end | |
def fib_tail(n) do | |
fib_tail(n, 1, 0) | |
end | |
defp fib_tail(0, _, _) do | |
0 | |
end | |
defp fib_tail(1, a, b) do | |
a + b | |
end | |
defp fib_tail(n, a, b) when n > 1 do | |
fib_tail(n - 1, b, a + b) | |
end | |
end |
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
// facs | |
// 0, 1, 2, 3, 4, 5, 6, 7, 8 | |
// 0, 1, 2, 6, 24, 120, 720, 5040, 40320 | |
function fac(n) { | |
if (n === 1) { | |
return n; | |
} | |
return n * fac(n - 1); | |
} | |
let facRes = fac(4); | |
console.log(facRes); | |
function facTail(n, memo) { | |
memo = memo || 1; | |
if (n === 1) { | |
return memo; | |
} | |
return facTail(n - 1, n * memo) | |
} | |
let facTailRes = facTail(4); | |
console.log(facTailRes); | |
// fibs | |
// 0, 1, 2, 3, 4, 5, 6, 7, 8 | |
// 0, 1, 1, 2, 3, 5, 8, 13, 21 | |
function fib(n) { | |
if (n === 0) { | |
return 0; | |
} | |
if (n === 1) { | |
return 1; | |
} | |
return fib(n - 1) + fib(n - 2); | |
} | |
let fibRes = fib(8); | |
console.log(fibRes); | |
function fibTail(n, a, b) { | |
a = a || 0; | |
b = b || 1; | |
if (n === 0) { | |
return 0; | |
} | |
if (n === 1) { | |
return 1; | |
} | |
if (n === 2) { | |
return a + b; | |
} | |
return fibTail(n - 1, b, a + b); | |
} | |
let fibTailRes = fibTail(8); | |
console.log(fibTailRes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment