Question

Problem 1: In class we saw that multiplying two n-bit numbers can be done in O(n^2)....

Problem 1: In class we saw that multiplying two n-bit numbers can be done in O(n^2). What about long division? Show an algorithm that on input integers a, b where a is n-bit long and b < a, computes a mod b in O(n^2).

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

ANSWER:

GIVEN THAT:

1. As you already know, normally our childhood division process is a recursive procedure.

We atart division one number, then after getting remainder, we will agiain divide that number until to get remainder 0 or not. so every time it will call recursive loops.

SO ALGORITHM FOR DIVISION IN AN RECURSIVE FASHION.

function LongDivision(a, b)
Input: x and y are Two n-bit integers where y ≥ 1 and b < a
Output: remainder and quotient after division process ... q -> quotienr, r -> remainder

//checking for empty divisor
if a = 0: return (q, r) = (0, 0)
//calling recursively
(q, r) = LongDivision([a/2], b)
//updating quotient and remainder
q = 2q, r = 2r
if x is odd: r = r + 1
if r ≥ b: r = r − b, q = q + 1
//finally returning result
return (q, r)

SINCE WE ARE CALLING RECURSIVELY, EVERY TIME IT WILL LOOP ITSELF AND CALL RECURSIVE FUNCTION. SO TWO STEPS NXN STEPS =N^2.

IN THIS WAY SO MANY RECURSIVE CALLS ARE MADE UNTIL TO GET REMAINDER 0.

SO N^2+N^2+

=O(n^2)

Add a comment
Know the answer?
Add Answer to:
Problem 1: In class we saw that multiplying two n-bit numbers can be done in O(n^2)....
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