Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

Re: Changing Pure Function in Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29832] Re: Changing Pure Function in Module
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Fri, 13 Jul 2001 04:19:14 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <9ijhui$ri5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

> I have the following function embedded in the findD code below.
> 
> a[1] =5;
> 
> a[n_]:= a[n] = (-1)^(n+1) (Abs[a[n-1]]+2)
> 
> In[225]:=
> findD[n_]:=
> Module[{c=0},NestWhile[(c++;(-1)^c(Abs[#]+2))&,5,JacobiSymbol[#,n]!=-1&]]
> 
> I would like to change the pure function to use the following (cleaner) function
> that requires no starting value (and starts for n = 1).
> 
> In[255]:=
> a[n_]:= a[n]= (-1)^(n+1)*(2n + 3)

So just do it, but with 

a[n_Integer]/; Positive[n]:= a[n]= (-1)^(n+1)*(2n + 3)


the code is *not* "cleaner" it is just with out the recursion.

Why not use

a[n_Integer] /; Positive[n] := a[n] = (-1)^(n + 1)*(2n + 3)

and

findD1[n_] /; Head[Sqrt[n]] =!= Integer :=
  Module[{i = 1, ai},
    While[True,
      If[JacobiSymbol[ ai = a[i++] , n] == -1, Break[]];
      ];
    ai
   ]


> 
> Also, it would be important to test n prior to calling findD.  If n is a perfect
> square, findD will "never" converge.  Is there a simple way to test if n is
> square 

PerfectSquareQ[n_Integer]:=Head[Sqrt[n]] === Integer


Regards
  Jens


  • Prev by Date: Re: Naming pieces of patterns
  • Next by Date: Re: kernel problem for trigonometry
  • Previous by thread: Re: Changing Pure Function in Module
  • Next by thread: Re: Changing Pure Function in Module