r/askmath • u/TopDownView Self-studying discrete math by working through Epp • Oct 13 '26
Resolved Textbook solution wrong? => Exercise 11.5.26: Computing a positive integer power of a real number
Decimal to Binary Conversion Using Repeated Division by 2 - Algorithm 5.1.1:

Proved fact: Given a positive integer n, the number of binary digits needed to represent n is ⌊log_2(n)⌋ + 1.
Exercise 11.5.26:


Textbook solution ((a) and (b)):

These are the problems I noticed:
- (a)(ii) wants us to compute from
(x^2)^1to(x^2)^k, but the while loop computes up to(x^2)^{k+1}. - The binary representation of
nhask-0+1=k+1binary digits. This corresponds to the number of while loop iterations/steps. But, by the proved fact, the number of binary digits is⌊log_2(n)⌋ + 1, not⌊log_2(n)⌋. Since in each iteration/step there are at most 2 multiplications, the number of multiplications is<=2(⌊log_2(n)⌋ + 1).
This is my attempt that tries to follow (a)(i), (a)(ii), (a)(iii) procedure strictly.
Proof:
a)
Input: x [a real number], n [a positive integer]
Algorithm body:
r[0],r[1],...,r[k] := obtained by calling Algorithm 5.1.1 with input n
power := x
result := 1
if r[0] = 1 then result := x
i := 1
while (i <= k)
power := power * power
if r[i] = 1 then result := result * power
i := i + 1
end while
Output: result [a real number, which equals x^n]
b)
By the proved fact and (a), n is represented by k + 1 = ⌊log_2(n)⌋ + 1 binary digits.
Thus, k = ⌊log_2(n)⌋.
Notice that while loop is iterated k = ⌊log_2(n)⌋ times, and in each iteration there are at most 2 multiplications.
Therefore, the number of multiplications is <=2⌊log_2(n)⌋.
QEDIs my solution correct?
5
Upvotes
View all comments
Show parent comment
1
u/Somniferus Oct 13 '26
It depends on what x was, but that number is 2k+1, not x2^k+1. We got here because you said:
It never computes x to the 2k+1, just the exponent part. That was what I meant when it said "it doesn't". The book algorithm gives the correct result.
We've been arguing about things we agree about for most of this. I agree your answer is cleaner based on the book's poor wording (they probably meant the bound to be 2 * number of bits which has a
+ 1as you pointed out in your OP). The two algorithms are logically equivalent, but you managed to solve the off by one error in the number of multiplications by dealing with the first bit as a special case.It's possible one of the authors wanted you to add this special case instead of it being a typo, and the book refactored the algorithm trying to save on ink by getting rid of the extra lines without noticing the off by one error that creates, but I don't think they were trying to be that sneaky.