Does append in place really work, or am I missing something?

Here is the do-it-yourself memoization for the Fibonacci function from

http://www.jsoftware.com/jwiki/Essays/Fibonacci_Sequence

F=: 0 1x
f0c=: 3 : 0
 if. y >: #F do. F=: F,(1+y-#F)$_1 end.
 if. 0 <: y{F do. y{F return. end.
 F=: F y}~ t=. 1e6 | (y-2) +&f0c (y-1)
 t
)

I have modified only with the mod 1e6: this is to keep numbers a
sensible size.  I now test it with various arguments:

test=:3 : 0
t=.0$0
for_p. 1e3*>:i.10 do.
F=:0 1x
t=.t, 6!:2 'f0c"0  i. p'
end.
)

diff=:2 -~/\ ]


]t=:test''
0.21368 0.72439 1.53614 2.65898 4.06148 5.77984 7.81953 10.1861 12.8458
15.9044
   diff t
0.51071 0.811749 1.12284 1.4025 1.71836 2.03969 2.36657 2.65966 3.0586
   diff^:2 t
0.301039 0.31109 0.27966 0.315863 0.321328 0.326884 0.293085 0.39894


The times look quadratic to me.  Anyone have any thoughts?

Best wishes,

John


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to