Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is what Fibonacci looks like, apparently

    f1a=: 3 : 0
     {. y f1a 0 1x
    :
     if. *x do. (x-1) f1a +/\|.y else. y end.
    )
 
    f1b=: {.@($:&0 1x) : ((<:@[ $: +/\@|.@])^:(*@[))
I think I have PTSD already just from reading this stuff.


APL without native fonts isn't fun at all. This is real fibonacci in APL:

http://progopedia.com/example/fibonacci/452/

http://dfns.dyalog.com/n_fibonacci.htm

This is a nice introduction into APL:

https://www.youtube.com/watch?v=_DTpQ4Kk2wA

And this is how APL is applied in the right way:

https://www.youtube.com/watch?v=a9xAKttWgP4


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?


There are some other example implementations here:

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│




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: