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
Oldest comments (51)
I will be starting with python.
I'll continue with javascript.
(Look mum, with a single expression function!)
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 :)
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 am using Kotlin here.
And your point is...?
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 ;)
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.
Since I doubt anybody else is going to do it, here is an implementation in Scheme
Tested for values of
n
up to10001
.I see what you did there. Trying to troll languages that you think may not have Int128 support :D. Nice try!
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.You said you doubt it can compute fib(10000). What were your doubts based on if you have no clue about C#? Plain trolling.
I'm just letting everyone (else) know I'm not reacting to his childish unsuccessfull trolling attempts anymore... I'm not his mother to raise him to his adulthood.
I know my shit, anyone who writes C# can see it as well. I will now just silently laugh...
Technically lazy evaluation of a endless stream is iterative, not recursive, but anyway, here is a solution in Haskell:
Read The Culture Map? :)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.