From: "Mosconi Tiziano" 
To: was@math.harvard.edu
Date: 2004-09-13 11:08 am

Dear Mr Stein

I'm an Italian guy, 27 years old, mechanical engineer keen on mathematics.

First of all I would like to thank you for the great book you have
written and put on your home page for downloading.

Few people understands that the download doesn't discourage the
purchase of the book and who is really interested in the subject buys
it while who isn't interested finds other ways to steal the knowledge.

I'd like to contribute to the book as much I can, and so I start from
what I know quite well, i.e. Mathematica.

In PartIII of the book (unfinished, I know) you start comparing three
simple programs written in Mathematica, Maple, Pari and Magma.

The first one is

f:=Function[n,t:=0;For[x=1,x<=n,x++,If[PrimeQ[x^2+1],t++]];t];

Timing[f[10^5]]

{4.226 Second,6656}

As a general rule, Mathematica does best working on the complete list
instead of on the elements of the list.


So usually C-like constructs like For, Do, While, are better replaced
by operations on the list.

Here is my proposal

f2[x_Integer]:=Module[{n=x},1+Count[PrimeQ[Range[2,n,2]^2+1],True]]

Timing[f2[100000]]

{2.514 Second,6656}

Trying to 'squeeze' the last bit from Mathematica I thought the
following program. It's a little faster, but the improvement isn't
worth the complication

f3[x_Integer]:=Module[{n=x},2+Count[PrimeQ[Flatten[Range[{0,4,6},n,10]]^2+1],True]]

Timing[f3[10^5]]

{2.313 Second,6656}

The second function, that about the Goldbach conjecture, cannot be
improved significantly because you don't know in advance the length of
the list you have to examine.

The third function, instead, can be improved considerably.

t:=Function[n,s:=0;For[x=3,x<=n,x++,If[PrimeQ[x]&&PrimeQ[x+2],s++]];s];

Timing[t[10^5]]

{1.552 Second,1224}

t2[x_Integer]:=Module[{n=x},Count[PrimeQ[Flatten[Position[PrimeQ[Range[1,n,2]],True]]*2+1],True]]


Timing[t2[10^5]]

{0.36 Second,1224}

If one use the built-in function PrimePi, the function becomes even faster.

t3[x_Integer]:=Module[{n=x},Count[PrimeQ[Table[Prime[i],{i,1,PrimePi[n]}]+2],True]]

Timing[t3[10^5]]

{0.08 Second,1224}

I hope to have been useful to you, and I'll be honoured if you'll ask
to me clarifications or further contributions.

Thanks again for your fantastic book.

Regards,
Tiziano Mosconi