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++:
I did this some time ago in Rust.
My first approach (the naive one) is very similar to @rapidnerd 's (George Marr's) solution in R and @andreanidouglas ' (Douglas R Andreani's) solution in Python:
I realized that this solution quickly exceeded in execution time for n > 40, so I optimized it using a mathematical theorem:
This means, we can break down numbers with odd indices into to numbers with about the half of the index – and do this again and again and again. This dramatically reduces execution time:
Inspired by @avalander 's answer, I also added this even faster algorithm:
fib_fast
andfib_super_fast
execute within microseconds even for higher indices. However, indices of 94 and higher cause a crash:Some performance measurements:
Just for a nice style, you could factor out the
return
s asif
is an expression...Nice!
Since writing my implementation I've remembered that the parameter
i
is unnecessary and you can decreasen
instead until it's 0.By the way, you didn't set default values for
curr
andprev
, how does that work, do you need to call it withfib_super_fast(n, 1, 0)
?Yes, I do. But I have to use fib_super_fast(n, 1, 1, 0) as my other implementations also begin with 1. The code for calling the fibonacci methods is:
Common Lisp!
(and for the TCO crowd... it's implementation dependent, so YMMV)
Respectfully, if you want to help newbies on a general issue such as recursion, just make a top-level comment addressing this problem. Show an example of the problem and suggest ways it can be fixed. That way, if the community deems the comment to be helpful, it will bubble up near the top of the discussion and it will be one of the first comments that newbies encounter.
In this manner, you won't be repeating the same comment all over the place, and you will have something clear that novices can wrap their minds around. In my opinion, making grumpy statements to the effect of "this crashes when the input reaches X" doesn't really help the very novices you say you most want to reach.
Edit: Added "in my opinion" :)
@mudasobwa I think pointing out a defect, or even just something that can be improved is fine. However I do think that making your point once is good enough. Also I recommend an approach where you say something like “this works fine when N is small enough, but you can extend the domain of this function to larger input values if you avoid using recursion.” If you can make your point while remaining friendly, why not do so?
I'll continue with it in R
Some comments may only be visible to logged-in visitors. Sign in to view all comments.