[Maxima] counting primes
stanislav.maslovski at gmail.com
Sat Apr 3 14:34:27 CDT 2010
On Sat, Apr 03, 2010 at 11:18:45PM +0400, Stanislav Maslovski wrote:
> Well, I can do that too in this Maxima, but for some reason only
> gradually. That is, if try it in one step, it fails:
> (%i1) pi[n]:=if n = 2 then 1 elseif primep(n) then pi[n-1]+1 else
> (%i2) pi;
> Maxima encountered a Lisp error:
> Error in PROGN [or a callee]: Bind stack overflow.
> Automatically continuing.
> To enable the Lisp debugger set *debugger-hook* to nil.
> Now, if I restart Maxima and do this, it works:
> (%i1) pi[n]:=if n = 2 then 1 elseif primep(n) then pi[n-1]+1 else pi[n-1]$
> (%i2) for n: 100 thru 5000 step 100 do print(pi[n]);
I thought a little bit more about it and I think I understand now
what is going on. Basically, in a recursive function like that, the
pi[n]s are not evaluated (and therefore their values are not stored
anywhere for reuse) until stack is filled with nested calls for
pi, pi, ... pi. The fact that pi is an array function
does not prevent this from happening. Right?
More information about the Maxima