3 ms·
Hmm, that is pretty ugly compared to its APL derived cousins in Q/K/KDB... /first n+2 numbers of fibonacci sequence fibonacci:{{x,sum -2#x}/[x;0 1]}; And
by gd1 12y ago
Hmm, that is pretty ugly compared to its APL derived cousins in Q/K/KDB...
/first n+2 numbers of fibonacci sequence
fibonacci:{{x,sum -2#x}/[x;0 1]};
And then...
q) fibonacci[10]
0 1 1 2 3 5 8 13 21 34 55 89
I haven't seen much J before, is that really the best implementation?
- klibertp 12y agoThere are some other example implementations here: http://www.jsoftware.com/jwiki/Essays/Fibonacci%20Sequence http://www.jsoftware.com/jwiki/Essays/Fibonacci%20Sequence This: f0b=: (-&2 +&$: -&1) ^: (1&<) M. is probably the most elegant solution. It's recursive ($: stands for "self-reference"), memoized, and it works: f0b&.> i.20 │0│1│1│2│3│5│8│13│21│34│55│89│144│233│377│610│987│1597│2584│4181│