public static TElement Pow<TElement>(

this IMonoid<TElement> monoid, TElement e, uint n)

where TElement : IEquatable<TElement>

{

TElement prod;


if (n == 0) return monoid.One;


if ((1 & n) != 0) prod = e;

else prod = monoid.One;


for (uint bit = 2; bit <= n; bit <<= 1)

{

e = monoid.Mult(e, e);


if ((bit & n) != 0) prod = monoid.Mult(prod, e);

}


return prod;

}