- #1
s3a
- 818
- 8
Homework Statement
Here is the algorithm (which works perfectly) for computing a^n for any integer a and a nonnegative integer n in Pascal-style pseudocode.:
Code:
k := n; b := 1; c:= a;
{a^n = b * (c^k)}
while k <> 0 do begin
if k mod 2 = 0 then begin
k := k div 2;
c := c * c;
end else begin
k := k - 1;
b := b * c;
end;
end;
Homework Equations
The given algorithm for computing a^n, where a is any integer and n is a nonnegative integer.
The Attempt at a Solution
I just wanted to know why this code works.
In particular, could someone please help me understand the roles that
Code:
c := c * c;
Code:
b := b * c;
Any input would be GREATLY appreciated!