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

Click For Summary
SUMMARY

The forum discussion focuses on implementing an O(log n) algorithm for computing a^n using exponentiation by repeated squaring. The user provided two implementations in Java: one based on their own logic and another from a book. The user's implementation incorrectly computes the exponentiation, yielding 3^11 instead of the correct 3^7. The book's algorithm correctly utilizes a variable for the base and another for the result, ensuring accurate calculations by squaring the base and multiplying the result based on the bits of the exponent.

PREREQUISITES
  • Understanding of O(log n) algorithm complexity
  • Familiarity with Java programming language
  • Knowledge of exponentiation by repeated squaring technique
  • Basic understanding of integer arithmetic and bit manipulation
NEXT STEPS
  • Study the implementation of exponentiation by repeated squaring in various programming languages
  • Learn about bit manipulation techniques in Java
  • Explore optimization techniques for recursive algorithms
  • Investigate the mathematical foundations of logarithmic complexity algorithms
USEFUL FOR

Students in computer science, software developers working on algorithm optimization, and anyone interested in efficient computation methods for exponentiation.

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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K