summaryrefslogtreecommitdiff
path: root/fib/nim
diff options
context:
space:
mode:
authorj-james2022-11-15 07:55:42 +0000
committerj-james2022-11-15 07:55:42 +0000
commit667253f48d20f76aff54f7ce9464b13c169ad96e (patch)
tree7efd64cfa34f198ed400947a80e6936341716dbc /fib/nim
Add cursed Fibonacci implementations
Diffstat (limited to 'fib/nim')
-rw-r--r--fib/nim/fib.nim16
1 files changed, 16 insertions, 0 deletions
diff --git a/fib/nim/fib.nim b/fib/nim/fib.nim
new file mode 100644
index 0000000..fc193f6
--- /dev/null
+++ b/fib/nim/fib.nim
@@ -0,0 +1,16 @@
+func fib(n: Natural): Natural =
+ if n < 2:
+ return n
+ else:
+ return fib(n-1) + fib(n-2)
+
+func fib2(n: int, a = 0, b = 1): int =
+ return if n == 0: a else: fib2(n-1, b, a+b)
+
+iterator fib3: int =
+ var a = 0
+ var b = 1
+ while true:
+ yield a
+ swap a, b
+ b += a