title: Exponentiation by squaring
body: <p>While i was searching for  <a href="http://en.wikipedia.org/wiki/Exponentiation_by_squaring" rel="nofollow">Exponentiation by squaring</a> i got the recursive method there but then i stumbled upon this pseudo code , Which i'm unable to understand fully.</p>
  
  <pre><code>function powermod(base, exponent, modulus) {
      if (base &lt; 1 || exponent &lt; 0 || modulus &lt; 1)
          return -1
  
      result = 1;
      while (exponent &gt; 0) {
         if ((exponent % 2) == 1) {
             result = (result * base) % modulus;
         }
         base = (base * base) % modulus;
         exponent = floor(exponent / 2);
      }
      return result;
  } 
  </code></pre>
  
  <p>if you can give some insight in simple terms it will be of great help </p>