Re: implementing successive squaring algorithm

*To*: mathgroup at smc.vnet.net*Subject*: [mg23256] Re: [mg23218] implementing successive squaring algorithm*From*: Hartmut Wolf <hwolf at debis.com>*Date*: Sat, 29 Apr 2000 22:05:00 -0400 (EDT)*Organization*: debis Systemhaus*References*: <200004240512.BAA15333@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Matthew Herman schrieb: > > I was looking to implement the successive squaring algorithm (same as > PowerMod), and I seem to have it, but the program won't store the > modulus value. > I took apart the program to the base of the problem, if anyone has > suggestions let me know. > > pmod[a_,b_,m_]:=g[a,IntegerDigits[b,2],m]; > g[a_,blist_,m_]:=blist*Reverse[NestList[f,a,(Length[blist]-1)]]; > f[a_] := Mod[a^2, m] > > What happens is that I get the NestList with it not being evaluated for > m=modulus. > Dear Matt, perhaps the best Method is to put f as a pure function into your rhs (m must be visible in the definition, since it is a scoped pattern variable). g[a_, blist_, m_] := blist*Reverse[NestList[Mod[#^2, m] &, a, (Length[blist] - 1)]] or define f f[a_, m_] := Mod[a^2, m] g[a_, blist_, m_] := blist*Reverse[NestList[f[#, m] &, a, (Length[blist] - 1)]] Alternatively you may transfer the parameter via Block: Clear[f, g] f = Mod[#^2, m] & g[a_, blist_, mpar_] := Block[{m = mpar}, blist*Reverse[NestList[f, a, (Length[blist] - 1)]]] Kind regards, Hartmut

**References**:**implementing successive squaring algorithm***From:*"Matthew Herman" <Henayni@hotmail.com>