What is the running time of the following piece of code ?

int power(int b, int n)
{
     int p=1, q=b, m=n;
     while(m>0)
     {
          if(m%2!=0)
               p*=q;
          m/=2;
          q*=q;
     }
     return p;
}


Difficulty level
This exercise is mostly suitable for students
O(log(n))

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Diffraction ratio of two slots