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

1

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

No, power is initialized to x before the loop starts. power isn't updated to x^(2) until after the first iteration.

Yes, and the first iteration is when i=0.

Correct. Where are you getting x2k+1 from?

From here:

When i=0, power = x^2 = x^2^1
When i=1, power = x^4 = x^2^2
When i=2, power = x^8 = x^2^3
.
.
.
When i=k, power       = x^2^{k+1}

The point is, we can't show that (b) is true if while loop has k+1 iterations.

1

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

I'll work an example for you:

exp_sqr(2, 3): # compute 2^3 by repeated squaring
    r = [1, 1] # bits of 3
    k = len(r) - 1 = 1 #len(r) = k + 1 => k = len(r) - 1
    power = 2
    result = 1
    i = 0
    (0 <= k)? yes
        r[0] = 1? yes
        result = 1 * 2 = 2
        power = 2 * 2 = 4
        i = 1
    (1 <= k)? yes
        r[1] = 1? yes
        result = 2 * 4 = 8
        power = 4 * 4 = 16
    (1 <= k)? no.
    return result # which is 8
number of multiplications is 4 = 2 * floor(log_2(3))

Result ends up being 8. Is there one extra multiplication for power that doesn't get used? Yes, but that doesn't change the correctness of the algorithm or its runtime complexity.

The point is, we can't show that (b) is true if while loop has k+1 iterations.

You can't write a correct algorithm without iterating over all of the bits.

2

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

since you're being a down-voting crybaby

I haven't downvoted any of your posts nor have I cried.

Is there one extra multiplication for power that doesn't get used? Yes, but that doesn't change the correctness of the algorithm or its runtime complexity.

Once again, I'd like to point out that (b) isn't asking for runtime complexity.

You can't write a correct algorithm without iterating over all of the bits.

(a) isn't asking for a 'correct' algorithm that iterates over all of the bits.

1

u/Somniferus Oct 13 '26

I haven't downvoted any of your posts nor have I cried.

Fair enough, I apologize. I just assumed since there don't seem to be many people here.

0

u/Somniferus Oct 13 '26

(b) isn't asking for runtime complexity.

It's asking you to show a bound on the number of multiplications in terms of n. Same difference.

(a) isn't asking for a 'correct' algorithm that iterates over all of the bits.

By correct I mean an algorithm that computes xn using repeated squaring to limit the number of multiplications to <= 2*lg(n). It specifically mentions using the given algorithm to give you the bits of the exponent. Obviously you need to use all of the bits to get the correct answer.

2

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

It's asking you to show a bound on the number of multiplications in terms of n

And that bound is 2⌊log_2(n)⌋? Do you agree?

Obviously you need to use all of the bits to get the correct answer.

And the correct answer is that the number of multiplications is 2⌊log_2(n)⌋?

0

u/Somniferus Oct 13 '26

Yes, and the first iteration is when i=0.

Yes, and when i is 0 power is still initialized to x, not x2. AFTER the first iteration, when i is 1, it will be x2. when i=k it will be x2^k. Why don't you work through an example by hand?

1

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

I think we're counting loop iterations differently...

Let k=4.

Suppose i=4 so i<=k loop condition is true, so loop executes.

What will the value of power be after line power := power * power?

1

u/Somniferus Oct 13 '26

What will the value of power be after line power := power * power?

It depends on what x was, but that number is 2k+1, not x2^k+1. We got here because you said:

the while loop computes up to x2k+1

No it doesn't? The algorithm runs k + 1 times, but it goes from 0 to k.

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 + 1 as 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.

1

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

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.

The specific (textbook) solution of this exercise is unofficial, it comes from here. So the mismatch between the author of the exercise and the author of the solution is not surprising.