Hellow DEV community, how are you?
Can we challenge ourselves in this fun "game"?
Let's see how many languages we can enumerate by writing the Fibonacci recursive algorithm on the comments. Are you in?
EDIT: this is not about having the fast/better code, just to have some fun
Latest comments (51)
Argh, I was going to write a similar one in Elixir! I do like your join with the arrow!
An Emacs Lisp version. Warning!! This will likely freeze your Emacs!!
UPDATE
Just realized it counts as a Common Lisp version too
Lord, we got it, you're the best fibonnaci solver out there. Congratulations on this!
Here's a Python variant for quite large numbers. Alas, due to recursion depth restrictions, any new calculated Fibonacci number should not have a larger sequence position than about 500 more than that of the highest already calculated.
I have haskell here with the best form of recursion: Self-recursion!
Here
fibs
is an infinite List of all fibonacci numbers, the!!
function returns the nth element of the list.I also have the boring tail recursive version here:
Modern C++ can do it more simply, though - this uses constexpr to tell the compiler that it can calculate these at build time if it wants. Using a constexpr arg (here a literal), it probably will do, but we could also pass in a variable to make it calculate it at runtime.
C++ can, of course, do it at build time with a bit of templates.
Here, we're definitely recursing - twice, because it's simpler. At runtime, it's just printing the value out that's been calculated by the compiler during the build.
Quick bit of Python. The error handling probably isn't complete, but Python has the useful benefit here that it has native, transparent, Big Number support - you can do fib(10000) if you want (or much higher if you have the memory).
But yeah, I just noticed Douglas wanted a recusrive algorithm, whereas I've done it iteratively without thinking. In general, iterative solutions will execute faster - though this isn't always the case.
As a Ruby fan, I'm sure glad Ruby has such a great ambassador in here.
Funge++:
Some comments may only be visible to logged-in visitors. Sign in to view all comments.