Вы находитесь на странице: 1из 6

28/11/2017 Fast Exponentiation Algorithms | Programming Logic

Programming Logic
Algorithms, Computer Science and Programming Puzzles

Fast Exponentiation Algorithms

Exponentiation is a very common part of mathematics, and it’s involved in many programming puzzles. If
you don’t have a function already implemented for you, a simple algorithm to compute a^b (a to the power
of b) would be:

int expo(int a, int b){


    int result = 1;

    while (b>0){
        result *= a;
        b--;
    }

    return result;
}

While this algorithm is relatively e cient, performing in liner time or O(n), it could be improved. In fact we
could do the same task in O(log(n)+log(n)). How? By using a method called exponentiation by squaring.

Here’s the basic idea: for any a^b, if a b is even we could write it as (a^b/2)^2. If b is odd, on the other hand,
we could write it as a * (a^(b-1/2))^2. Here’s a picture from Wikipedia which better illustrates it:

At each step we pretty much cut the problem in half, adding an extra multiplication for odd numbers. The
recursive algorithm in C looks like this:

https://www.programminglogic.com/fast-exponentiation-algorithms/ 1/6
28/11/2017 Fast Exponentiation Algorithms | Programming Logic

int expo(int a, int b){


    if (b==1)
        return a;
    if (b==2)
        return a*a;

    if (b%2==0){
            return expo(expo(a,b/2),2);
    }
    else{
        return a*expo(expo(a,(b-1)/2),2);
    }

To test both algorithms I elevated every number from 1 up to 100,000,000 to the power of 30. Using the
naive approach it took 7.1 seconds. Using the exponentiation by squaring one it took 3.9 seconds.

We can also treat the case where b is odd by re-writing it as a^b = a * a^(b-1), and break the treatment of
even powers in two steps. This makes the algorithm easier to understand and a bit more e cient (surpris-
ingly). The algorithm below took 3.4 seconds to complete the same task as above.

int expo(int a, int b){


    int result;
    
    if (b==2)
        return a * a;

    if (b==1)
        return a;

    if (b%2==1){
        return a * expo(a,b-1);
    }
    else{
        result = expo(a,b/2);
        return result * result;
    }
}

https://www.programminglogic.com/fast-exponentiation-algorithms/ 2/6
28/11/2017 Fast Exponentiation Algorithms | Programming Logic

And nally here’s the most optimized exponentiation by squaring algorithm I have seen around. It’s an itera-
tive version where at each step you divide the exponent by two and square the base, and then for the itera-
tions where the exponent is odd you multiply the result by the base. In theory at the odd iterations you
would need to decrement the exponent by one, but this is not necessary because we’ll be performing inte-
ger division (i.e., 5/2 is equal to 4/2). The algorithm below took 2.2 seconds to complete the sake task as
above:

int expo(int a, int b){


  int result = 1;

  while (b){
    if (b%2==1){
      result *= a;
    }
    b /= 2;
    a *= a;
  }

  return result;
}

We can squeeze a bit more of performance by using bitwise operations at the mod and divisor operators (it
saves 0.15 seconds on the same task):

int expo(int a, int b){


  int result = 1;

  while (b){
    if (b&1){
      result *= a;
    }
    b >>=1 ;
    a *= a;
  }

  return result;
}

By the way, elevating all numbers from 1 to 100,000,000 to the power of 30 took 5.6 seconds with the stan-
dard library’s pow function. Sure, it uses double, but still it proves that if you are working with integers you

https://www.programminglogic.com/fast-exponentiation-algorithms/ 3/6
28/11/2017 Fast Exponentiation Algorithms | Programming Logic

might as well implement your own exponentiation function to make things more e cient.

There are some algorithms that are slightly more e cient, but they are much more complicated, so not
worth the trouble in most cases. If you want to check them, though, visit the Wikipedia link above.

This entry was posted in Algorithms, Number Theory on December 15, 2011 [https://www.programminglog-
ic.com/fast-exponentiation-algorithms/] by Daniel Scocco.

9 thoughts on “Fast Exponentiation Algorithms”

Alex
September 27, 2013 at 4:19 pm

Thanks for posting! However, if you use the same code in Java, it only computes up to the 10th power I
think.

Bharath
October 19, 2013 at 4:37 am

Good One.nice.

Nikhil
November 7, 2013 at 9:09 am

Thanks!! It really helped a lot!! Great Algo!!

Farid
January 20, 2014 at 3:13 pm

Nice! Thanks for sharing!

https://www.programminglogic.com/fast-exponentiation-algorithms/ 4/6
28/11/2017 Fast Exponentiation Algorithms | Programming Logic

There is a typo on the last code (replacing /2 with shifting right):

if (b%2==1)

instead of

if (b%1)

akhila
June 22, 2017 at 1:18 am

thanks it worked! you are a god coder!

Reaga
February 20, 2014 at 7:12 pm

Ok, had HW to implement fast exponentiation w/o recursion, used the second to last code. But I have a
question:

I understand the algorithm. From a logical and mathematical point of view, it makes perfect sense. But I
don’t understand the code.

Can someone explain this: We mention result 3x.


1. Initiation: int result = 1;
2. Returning: return result;
3. extra multiplication in case of odd exponent: result *= a;

Only in number 3 do we actively change result, so I can only fathom 2 results from this function: 1 and a (de-
spite actually seeing this code work properly). So, how does result change in this code?

anonymous
March 16, 2014 at 6:58 am

Boss can you explain me the fastest method of multiplying huge numbers (20to30digits long) in very very
less time(for eg. Schonage Strassen algorithm). I read it on wikipedia but i didn’t understand various impor-
tant concepts in it like fast fourier transform,discrete fourier transform,convolutions,etc. It was very very

https://www.programminglogic.com/fast-exponentiation-algorithms/ 5/6
28/11/2017 Fast Exponentiation Algorithms | Programming Logic

di cult for me to understand may be because i am too small to learn those concepts. I am searching for
fastest multiplication method ever but i am unsuccessful. I love arithmetic very much so i am trying to learn
these things though they are beyond my understanding but i have ability I want very very simple explana-
tion .I will be very very very very helpful to you for any help that you may consider worthy. I hope you will
surely help me.And thanks for such a beautiful explanation of exponentiation by squaring.

Feroz Ahmad
June 11, 2014 at 11:40 pm

Very well! Thanks a lot!

Well this may work even faster :

int expo(int a, int b) //a-base b-power result-answer


{ long result;
result=a;
if(b==0)
{ return 1;
}
while(b>1)
{ if(b%2==0)
{ result=result*result;
}
else
result=result*result*a;

b/=2;
}
return result;
}

hawk eye
June 22, 2015 at 5:42 am

feroz, that won’t work.

https://www.programminglogic.com/fast-exponentiation-algorithms/ 6/6

Вам также может понравиться