Question about O(log n) algorithm for computing a^n

Click For Summary
The discussion focuses on implementing an O(log n) algorithm for computing a^n using exponentiation by repeated squaring. Two approaches are presented: a personal attempt and a book's solution. The main issue identified is that the personal algorithm incorrectly accumulates the result by multiplying the base too many times, leading to incorrect outputs, such as calculating 3^11 instead of 3^7. The book's method correctly maintains a separate variable for the result and squares the base appropriately based on the exponent's parity. Clarification is sought on the logic flaw in the personal implementation compared to the book's approach.
s3a
Messages
828
Reaction score
8

Homework Statement


Let a be an integer and n be a non-negative integer.

Without changing the values of a and n, produce an algorithm, of O(log n) complexity, which computes a^n.

Homework Equations


For integers a, n and i, where n >= 0 and is even, where i >= 1 and where a has no further constraints:
a^n = (a^[2i])^(n/[2i])

For integers a, n and i, where n >= 0 and is odd, where i >= 1 and where a has no further constraints:
a^n = a^(n-i) * a^i

The Attempt at a Solution


The following is the solution I attempted by myself, as well as the one provided by a book I'm using.:
Code:
    public static void myWay(int a, int n) {
       
        int b = a;
        int k = n;
       
        while( k != 0 ) {
           
            if(k % 2 == 0) {
               
                b = b * b;
                k = k / 2;
            }
            else {
               
                b = b * a;
                k = k - 1;
            }
        }
       
        System.out.println(b);
    }
   
    public static void wayOfBook(int a, int n) {
       
        int k = n, b = 1, c = a;
       
        while(k != 0) {
           
            if(k % 2 == 0) {
               
                k = k / 2;
                c = c * c;
            }
            else {
               
                k = k - 1;
                b = b * c;
            }
        }
       
        System.out.println(b);
    }

I understand what the algorithm of the book is doing, but what I don't understand is why what I am doing is wrong.

Could someone please point out the flaw with my logic?

For instance, if I pass a = 3 and n = 7, my algorithm prints 3^11 = 177147 (which is incorrect), whereas the book's algorithm prints out 3^7 = 2187 (which is correct).

Any input would be GREATLY appreciated!
 
Physics news on Phys.org
The method is called exponentiation by repeated squaring. One variable is squared every loop, the result variable is multiplied by the repeatedly squared variable for each 1 bit in the exponent, starting with the least significant bit.

https://en.wikipedia.org/wiki/Exponentiation_by_squaring
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K