MathGroup Archive 2000

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

Search the Archive

Re: implementing successive squaring algorithm

  • To: mathgroup at
  • Subject: [mg23256] Re: [mg23218] implementing successive squaring algorithm
  • From: Hartmut Wolf <hwolf at>
  • Date: Sat, 29 Apr 2000 22:05:00 -0400 (EDT)
  • Organization: debis Systemhaus
  • References: <>
  • Sender: owner-wri-mathgroup at

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

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

  • Prev by Date: missing images
  • Next by Date: pure functions
  • Previous by thread: implementing successive squaring algorithm
  • Next by thread: copying notebook sections to text documents