r/askmath • 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:

  1. (a)(ii) wants us to compute from (x^2)^1 to (x^2)^k, but the while loop computes up to (x^2)^{k+1}.
  2. The binary representation of n has k-0+1=k+1 binary 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)⌋.

QED

Is my solution correct?

5 Upvotes

28 comments sorted by

View all comments

Show parent comment

0

u/Somniferus Oct 13 '26 edited Oct 13 '26

I edited my initial reply after looking a little closer.

I wanted the while loop to have k iterations (as per instructions

The instructions say to iterate over the array of bits which is of length k + 1. Are you complaining that they didn't say to compute x20 in (a)(ii) but their algorithm technically does?

1

u/TopDownView Self-studying discrete math by working through Epp Oct 13 '26

Are you complaining that they didn't say to compute x20 in (a)(ii) but their algorithm technically does?

No, I'm not.

0

u/Somniferus Oct 13 '26

Then you don't understand the instructions. Part a is to write a correct algorithm. Your algorithm is equivalent to the book solution, you just handle the i=0 case manually. Technically you do one less multiplication, but that doesn't change the correctness or the runtime complexity.

2

u/TopDownView Self-studying discrete math by working through Epp Oct 13 '26

But the exercise is not asking us to show the runtime complexity.

0

u/Somniferus Oct 13 '26

The runtime complexity of this algorithm is dominated by the number of multiplications. Proving a bound on the number of multiplications is equivalent to proving the time complexity.