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
Top comments (51)
I'll continue with javascript.
(Look mum, with a single expression function!)
To be absolutely pedantic, this code is valid and will work with large numbers because it is written in EcmaScript 6. Part of the ES6 standard is for the language interpreter to implement tail call optimization (TCO). EcmaScript is a language defined by a standard, not by an implementation.
Now, at present, barely any interpreters implement TCO - here's a fun comparison table to see which do or don't. This makes me sad, but there we are.
But, as I say, the function above will work just fine according to the language standard. Whether this code blows the call stack of your interpreter is dependent on whether your interpreter implements the full ES6 standard. Which it probably doesn't.
To put it strongly, complaining that this code doesn't run on an JS interpreter without TCO is like complaining that it can't run by a Ruby interpreter. The fault is with the interpreter, not the code.
I like this! Don't think you need the outer parens though :D
Should work without, I'm just used to wrapping ternary expressions between them for some reason and I find it weird without :)
I haven't seen a SQL implementation yet, so here's one:
Here's interesting one for SQL Server with maximum value before arithmetic overflow due to data type limit:
Here it is in Oracle SQL:
The limit of 605 is the limit before numeric overflow.
👏👏 I never thought you could do that with SQL!
Fuck yes SQL!
I feel like every time someone responds to one of these with a SQL implementation that a) my heart grows a few sizes and b) they earn extra special bonus points in my head because hell yeah.
⭐️
(nonironic gold star, fwiw)
I'll follow up in C# (and LINQ):
(Look ma', single expression lambda without recursion!)
Imaginative, I like it (even if it's not recursive :P)!
I lied a bit. The recursion is there, hidden in the Aggregate function. It's the framework abstraction that masks it.
Otherwise it wouldn't qualify as an answer to OP challenge, or would it? ;)
I know nothing about C#, but I thought that the
Aggregate
function was somehow invoking an anonymous function here that given current and previous values returns the next element. If that's the case, and it's a function calling another function multiple times instead of the function invoking itself, can we still call it recursion?The
Aggregate
function is member of theEnumerable
type and applies an anonymous accumulator function looping all elements in that "array/set". Hard to tell if we can treat it as a recursion. I would have to look at the stack if there are pointers left to the originating caller.Fibonacci's sequence is strictly sequential so it works well but for more parallel calculations, like SUM, AVG, MAX, MIN accumulator functions you can do:
And it will multithread across many OS threads to achieve the best efficiency.
Edit: This is however not plain C# .Net anymore but LINQ ;)
Since I doubt anybody else is going to do it, here is an implementation in Scheme
Tested for values of
n
up to10001
.Please do not feed this troll.
He picked fib(10000) because it blows Int64 and now he's trying to victoriously convince everyone their programming language of choice is crap.
The same as if I would say I can compute and save a file containing Pi to 1e+13 numbers and don't tell anyone I have 12TB NAS storage attached...
Aleksei, if you try to work with very large numbers in Javascript, at some point (before the 10000th element in the fibonacci series) it just returns
Infinity
because it can't deal with them.man, just sit down and relax. there is no point for all this harassement.
If you think it's important enough for a code challenge to have a Javascript implementation that returns
Infinity
instead of blowing the stack for large values, be my guest and suggest an alternative implementation.Just posting the console output of calling my function with an arbitrarily long value seems rather pointless. I agree my solution is not perfect but I'd appreciate a better solution more than an error message.
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:
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.
Technically lazy evaluation of a endless stream is iterative, not recursive, but anyway, here is a solution in Haskell:
Some comments may only be visible to logged-in visitors. Sign in to view all comments.