문제

Well.. I have started to learn APL since yesterday. I'm watching youtube videos teaching about various symbols from basic, and I'm using NARS2000.

What I want is to print the Fibonacci sequence. I know there's several codes, but since I haven't studied advanced things, I started to write my own code.

First I made this array: APL code: N{leftarrow}2{space}2{rho}1{space}1{space}1{space}0

The idea is simple : the element at (1,1) in Nⁿ is (n+1)th fibonacci sequence.

What I did was:

+.{times}\N{space}N{space}N{space}N{space}N

and

{uparrow}{dieresis}(+.{times}\N{space}N{space}N{space}N{space}N)

Well, it works. However, if I want 16th term, then I should do

Same as above, but here's a lot of Ns

What I need is to write arbitary amount of Ns. Of course I know about {rho}. However,

I used rho but this is not what I wanted.

(bottom was cut)

And I noticed that (i 2 2){rho}N and i Ns are different.

rho and multiple Ns

What operator I should use to do same thing as N N N...N does?

도움이 되었습니까?

해결책

You were almost there. ("reshape") is the right operator to use; however, you want it to treat your matrix N not as a matrix, but as a single, scalar element. For this purpose, you wrap it using the "enclose" operator :

      4⍴⊂N
  1 1    1 1    1 1    1 1 
  1 0    1 0    1 0    1 0

If we wrap this up, we arrive at (e.g.) the following expression:

      ↑¨+.×\16⍴⊂2 2⍴1 1 1 0
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

(Allow me one remark, though: by definition, the Fibonacci sequence starts with 0 and 1.)

다른 팁

If you want to start with 0 and 1, just use 0 1 1 1 instead of 1 1 1 0

      ↑¨+.×\16⍴⊂2 2⍴0 1 1 1
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top