Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lisp

Idiom #32 Integer exponentiation by squaring

Create function exp which calculates (fast) the value x power n.
x and n are non-negative integers.

(loop
  for (power rem) = (list p 0) then (multiple-value-list (floor power 2))
  for multiple = 1 then (if (zerop rem) multiple (* multiple base))
  for base = x then (* base base)
  when (= power 0) return 1
  when (= power 1) return (* multiple base)))

All of the odd values are multiplied into multiple while base is squared with each iteration.

Since specification is "non-negative" handle the case of the power being 0 separately
unsigned int exp(unsigned int x,unsigned int n)
{
    if(n==0)
    {
        return 1;
    }
    if(n==1)
    {
        return x;
    }
    if(!(n%2))
    {
        return exp(x*x,n/2);
    }
    return x*exp(x*x,(n-1)/2);
}

New implementation...