Create a Fibonacci Series without Loop or Flow Control in APL

Is there a way to create a fibonacci sequence in a single line APL that does not require loops or flow control?

I did this with a function and a conditional test, but I believe there should be a more elegant, declarative way. the example that I found that claims on this in one line do not work on gnu-apl - it seems that it is on the right track using mathematical mathematics, but it is not easy for me to follow it and cannot set it up correctly.

I am pursuing the APL as my first real programming language (I like the characters, I just do it.) I am now using Project Euler as a way to get to know each other better.

+4
source share
5 answers

I also like APL characters, as well as its programming power in an array. Other array languages ​​may be more powerful, such as J, but they lack the beauty of APL characters and explicit syntax.

I just tried the example you refer to in the GNU APL, and everything works correctly:

0 1↓↑+.×/5/2 21 1 1 0
50 1↓↑+.×/6/2 21 1 1 0
80 1↓↑+.×/7/2 21 1 1 0
13

If you cannot get it to work, make sure that:

  • enter (or copy and paste) the exact characters shown in the example: in particular ×- U + 00D7 MULTIPLICATION SIGN, not X; - U + 2374 APL FUNCTIONAL SYMBOL RHO, not any other Greek Rho, and definitely not P; - U + 2282 SUBSET OF; etc.
  • check some numbers other than 1 or 5, because they are the only numbers that are equal to their Fibonacci number;
  • N, , N, N←7 .

, , :

      2 2⍴1 1 1 0
1 1
1 0
      ⊂2 2⍴1 1 1 0
 1 1 
 1 0 
      7/⊂2 2⍴1 1 1 0
 1 1   1 1   1 1   1 1   1 1   1 1   1 1 
 1 0   1 0   1 0   1 0   1 0   1 0   1 0 
      +.×/7/⊂2 2⍴1 1 1 0
 21 13 
 13  8 
      ↑+.×/7/⊂2 2⍴1 1 1 0
21 13
13  8
      0 1↓↑+.×/7/⊂2 2⍴1 1 1 0
13
 8
      ↑0 1↓↑+.×/7/⊂2 2⍴1 1 1 0
13

, , (bravo @mappo!)

, , , , :

      2⌷∊+.×/7/⊂∘.∨⍨1 0
13

, 24 17 . , : -)

GNU APL , , Dyalog APL Programmer Guide Language Reference, .

+3

- ( ) . GNU APL, Dyalog ( 13.1) APN NGN.

 ({⍵,+/¯2↑⍵} ⍣ 20) (1 1)  

, .

({⍵,+/¯2↑⍵} ⍣ 3) (1 1)

{⍵,+/¯2↑⍵} {⍵,+/¯2↑⍵} {⍵,+/¯2↑⍵} 1 1

.

1 1 - , {⍵, +/¯2 ↑ ⍵} .

+2

, , N ( N- ) ( APL2):

+/¨(⊂0 0)⍉¨⊖¨(2/¨⍳N)↑¨⊂P←V∘.!V←⍳1+N←20

: . :

20 :

P←V∘.!V←⍳1+N←20

N :

(2/¨⍳N)↑¨⊂P 

:

(⊂0 0)⍉

, .

, +/.

+2

Dyalog APL? power, ( , " Dyalog APL", . 416).

:

(+.×⍣10)⍨ 2 2⍴1 1 1 0

:

{⊃(+.×⍣⍵)⍨2 2⍴1 1 1 0} 10

power, ( GNU APL 1.5):

{+.×/⍵⍴⊂2 2⍴1 1 1 0} 10
+1

, , gnu-apl, , "neg-two take":

fib ← {{⍵, +/ ¯2↑ ⍵} / ⌽ ⍳⍵}

- iota, . :

      fib 10
 1 1 2 3 5 8 13 21 34 55
0

All Articles