MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Efficiency question


Thank you.  I stumbled upon Fold the same day I posted that question. 
That's a great function!

Interestingly, Mathematica doesn't take advantage of the gains to be had if n
is prime.

f[n_]:=If[PrimeQ[n], Sum[z^(k-1),{k,n}], Cyclotomic[n,z]]

This is quite a bit faster

********************************************************************
In[22]:=
\!\(f[n_] := If[PrimeQ[n], Sum[z\^\(k - 1\), {k, n}], Cyclotomic[n,
z]]\)

In[23]:=
Table[f[n]-Cyclotomic[n,z],{n,2,20}]

Out[23]=
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

In[24]:=
Timing[Table[Cyclotomic[n,z],{n,2,2000}];]

Out[24]=
{8.242 Second,Null}

In[25]:=
Timing[Table[f[n],{n,2,2000}];]

Out[25]=
{6.099 Second,Null}
*********************************************************************


  • Prev by Date: repeated entries in File>Palettes menu (again)
  • Next by Date: Package problem
  • Previous by thread: Re: Efficiency question
  • Next by thread: puzzling difference in speed