Here's a way to do it:
fib=: 3 : 'y,+/_2{.y'"1
fib 1 1
1 1 2
fib fib 1 1
1 1 2 3
fib^:10]1 1
1 1 2 3 5 8 13 21 34 55 89 144
2%~/\fib^:10]1 1
1 2 1.5 1.6666667 1.6 1.625 1.6153846 1.6190476 1.6176471 1.6181818
1.6179775
On Sun, Aug 16, 2009 at 5:11 PM, Martin Kreuzer <[email protected]> wrote:
>
> ...
> To give you an idea what I'm aiming at, here's
> some code which uses the "echo" ( and therefore
> will only work in JConsole, not JWD); my wish has been
> (1) to get rid of "echo" and
> (2) produce a "loopless" version of the code (as
> I've read -people seem to stress that all the
> time- that "loopless" is one of the key concepts in J).
>
> NB. *gra -- monad --------------------------------------------------
> NB. calculate Golden ration approx (by ratio of consecutive Fibonacci
> numbers)
> NB. throw an error for N > 23 (*fib restrainment)
> gra=: 3 : 0
> n=. y
> nmax=. 23
> if. n > nmax do.
> 'Error [gra]: parameter too large (', (":n), '>', (":nmax), ')'
> else.
> r=. 2 + i.(n-1)
> echo ' n gra'
> for_j. r do.
> fn=. fib j [ fn1=. fib (j-1)
> gr=. fn % fn1
> echo (3j0":j), (16j12":gr), (8j0":fn), '/', (":fn1)
> end.
> gr=. 0.5 * (1 + %:5)
> echo ' '
> echo ' gr', (16j12":gr)
> end.
> )
>
> M.
>
>
>
>
>
> ---
> avast! Antivirus: Outbound message clean.
> Virus Database (VPS): 090815-0, 15.08.2009
> Tested on: 16.08.2009 21:12:03
> avast! - copyright (c) 1988-2009 ALWIL Software.
> http://www.avast.com
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm