counting primes



Recursive functions are generally slow and inefficient, and relying on your
system to keep track of previously computed values can be dodgy.  I prefer a
two step approach:

(%i1) pr(n):=if primep(n) then 1 else 0;
(%o1)                 pr(n) := if primep(n) then 1 else 0
(%i2) countprimes(n):=sum(pr(i),i,1,n);
(%o2)                countprimes(n) := sum(pr(i), i, 1, n)
(%i3) countprimes(10000);
(%o3)                                 1229

cheers,
Alasdair

--
Blog: http://amca01.wordpress.com
Web:  http://bit.ly/Alasdair
Facebook: http://www.facebook.com/alasdair.mcandrew