aboutsummaryrefslogtreecommitdiff
path: root/entries/s-lando
diff options
context:
space:
mode:
authors-lando2022-11-13 23:01:02 +0000
committers-lando2022-11-13 23:01:02 +0000
commit1a7717e25e93599666803bb11f80c87117b01114 (patch)
tree08fda466d36c19896a3f1b078d961a0868ea0eae /entries/s-lando
parent7e8d796440aac327b911f86e6ed7502d7e095fcc (diff)
elixir fibs
Diffstat (limited to 'entries/s-lando')
-rw-r--r--entries/s-lando/fib.ex19
1 files changed, 19 insertions, 0 deletions
diff --git a/entries/s-lando/fib.ex b/entries/s-lando/fib.ex
new file mode 100644
index 0000000..e4783d6
--- /dev/null
+++ b/entries/s-lando/fib.ex
@@ -0,0 +1,19 @@
+defmodule Fib do
+ # elegant
+ def fib(n) when n < 2, do: n
+ def fib(n), do: fib(n - 1) + fib(n - 2)
+
+
+ # less elegant
+ def fib2(n) do
+ fib2(n, 0, 1)
+ end
+
+ def fib2(n, current, next) do
+ cond do
+ n == 0 -> current
+ n == 1 -> next
+ true -> fib2(n - 1, next, current + next)
+ end
+ end
+end