Question

Rewrite the LeftRightBinaryExponentiation algorithm on bottom to work for ? = 0 as well as any positive integer.

No credit will be given for answers that simply start with an if statement for n = 0.

ALGORITHM LefiRightBinary Exponentiation(a, b(n)) Computes a by the left-to-right binary exponentiation algorithm I/Input: A

I already tried the work that start with if statement. is there anyway can work without it?

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

let I be the length of list b(n) which represents the number n.

First we will have to check if all the digits in b(n) is 0 for it to work for n=0

LeftRightBinaryExponentiation(a, b(n))

product <---- a;

flag<--0

for i<-0 to I do

     if bi=1 flag=1 //flag will become 1 if any 1 is encountered thereby concluding n!=0

if flag<--0 //if flag is still 0 it means there is not a single 1 in the list thereby concluding that n=0

   product<--1 //because if n=0, a^n=a^0=1

else

      for i <--I -1 downto 0 do

            product <-- product * product;

            if bi ==1 product <-- product *a

return product

Add a comment
Know the answer?
Add Answer to:
Rewrite the LeftRightBinaryExponentiation algorithm on bottom to work for ? = 0 as well as any...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT