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).
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)
Problem 1: In class we saw that multiplying two n-bit numbers can be done in O(n^2)....