counting primes



Hello,

On Wed, Mar 31, 2010 at 12:38:46PM +0100, Leo Butler wrote: 
> On Wed, 31 Mar 2010, Jean Legeande wrote:
> 
> < Dear Maxima users,
> < ?
> < What is a good way to?code the prime-counting function with Maxima ?
>  
> How about:
> 
> pi[n]:=if n = 2 then 1 elseif primep(n) then pi[n-1]+1 else pi[n-1] ;

I tried it out of curiosity and got this:

Maxima 5.20.1 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.

(%i1) pi[n]:=if n = 2 then 1 elseif primep(n) then pi[n-1]+1 else pi[n-1]$

(%i2) pi[100];
(%o2)                                 25
(%i3) pi[500];

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.

But if I do it gradually 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 3000 step 100 do print(pi[n]);
25 
46 
62 
.
.
.
407 
419 
430

What is the reason?

-- 
Stanislav