Fibonacci numbers in APL
Example for versions
Dyalog APL 13.1
This example uses Binet’s formula implemented via an anonymous D-function. ⋄
is expression separator, so the function consists of two expressions evaluated in left-to-right order. The first one calculates golden ration and binds it to name phi
. The second one calculates the value of the function (Fibonacci number) based on its right argument ⍵
(the index of the number). ⌈
rounds the number up.
Since the function is unary and is defined using scalar functions only, it can be applied to an array — in this case to an array of indices between 1 and 16, inclusive. This will result in an array of Fibonacci numbers:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
{phi←(1+5*0.5)÷2 ⋄ ⌈((phi*⍵) - (1-phi)*⍵)÷5*0.5} 1+⍳16
Comments
]]>blog comments powered by Disqus
]]>