UVa 10023 - Square root

Square Root
Big Numbers

Big numbers are to be implemented to resolve this problem. The operations of multiplication and half part are enough for this purpose.

Big Numbers Multiplication

Assuming it is easy to sum up big numbers and to multiply them by an number of the form $$2^n$$, n being an integer, it becomes a trivial task to execute multiplication above these numbers.

To accomplish this task we shall consider two big numbers, a and b, which, represented as an array of integers (the least significant digit at the left) would appear as:

$$a = (a_0, a_1, a_2, ..., a_m-_1)), b = (b_0, b_1, b_2, ..., b_n-_1)$$

If both numbers are divided in half so that:

$$a = (A B), b = (C D),$$ (A, B, C, D are big numbers array representations),

we would notice that:

$$a = A + 2^x.B, b = C + 2^y.D,$$ x = digit count of a, y = digit count of b

Therefore the product sought would be so that:

$$a.b = A.C + 2^y.A.D + 2^x.B.C + 2^x.2^y.B.D$$

Make x = y.

$$a.b = A.C + 2^x.(A.D + B.C) + 2^x.2^y.B.D$$

$$a.b = $$

Then the recursive aproach shall take $$O(n.log(n))$$ steps to solve the problem. The argument is based on solving the recurrence s(n) such that:

$$s(n) = 2.s(n-1) + k.n,$$ being k a constant factor.

This recurrence represents the number of time steps taken to solve the problem since two multiplications which take $$s(n-1)$$ are held and the sum operations are of about $$k.n$$ steps.

Of course, the number n itself is of the order of $$2^x$$, the x would be such that $$x=O(log(n))$$. Then the final time amount taken to multiply those numbers together is of $$O(log(n).log(log(n)))$$.

Square Root Algorithm

A simple but effective square root algorithm is based on a binary search of the numbers from 1 to n.

In pseudo-code:

integer sqrt(integer n) { integer a, b, m; a:= 1; b:= n; loop { m:= (a+b)/2; if (m = a || m = b) return m;   if (m*m > n) a:= m;    else b:= m;  } }

Solution

Solution to this problem is a simple combination of the two above algorithms.

--Schultz 14:35, 14 Nov 2006 (EST)