0% found this document useful (0 votes)
28 views

Today's Material: - Long Integer Multiplication

This document discusses algorithms for multiplying long integers represented as arrays. A brute force iterative method takes O(n^2) time while a divide-and-conquer approach takes O(n^1.585) time but has higher overhead. The divide-and-conquer method is more efficient for integers larger than 50-260 digits depending on the overhead factor.

Uploaded by

Fatih Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Today's Material: - Long Integer Multiplication

This document discusses algorithms for multiplying long integers represented as arrays. A brute force iterative method takes O(n^2) time while a divide-and-conquer approach takes O(n^1.585) time but has higher overhead. The divide-and-conquer method is more efficient for integers larger than 50-260 digits depending on the overhead factor.

Uploaded by

Fatih Arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Today’s Material

• Long Integer Multiplication


– Iterative Algorithm
– Divide & Conquer Algorithm

1
Problem Definition
• Given two decimal numbers consisting of “N”
digits, design an algorithm to multiply them
efficiently

• What’s the maximum signed integer you can


represent in a 4 or 8 byte integer?
– 231 – 1 = 2147483647
– 263 – 1 = 9223372036854775807

• What if you want to work with very large


numbers
– Assume N = 100, 500 or 1000
2
Applications
• Cryptography
– Techniques for encryption/decryption are based on
number-theoretic techniques that use very large
numbers

– DES uses 56-bit keys


– RSA uses 1024 bit keys

– Bottom line is, we need efficient algorithms to


manipulate large numbers

3
Representing Long Integers
• You can simply use an array
– Each slot of the array keeps one digit of the number
– A[0] stores the least significant digit
– A[N-1] stores the most-significant digit

• Example:
– char A[] = {4, 1, 5, 3, 2}; // 23514
– char B[] = {9, 5, 2, 1}; // 1259

4
Operations on Long Integers
• Addition and subtraction is easy
– Simply add/subtract digit by digit – O(N)

• What about multiplication?


– Basic multiplication works digit-by-digit
– How much does this take? O(N2)

– Can we make this faster? How about a divide &


conquer strategy?

5
D&Q Long Integer Multiplication
n
n/2 n/2

w x A

y z B

wz xz

wy xy

wy wz + xz xz P=A*B

• Think of A and B consisting of two superdigits


of size roughly n/2 digits

6
D&Q Long Integer Multiplication
n
n/2 n/2

w x A

y z B

• Let m = n/2
• A = w*10m + x
• B = y*10m + z

• Mult(A, B) = Mult(w, y)*102m + (Mult(w, z) + Mult(x, y))*10m + Mult(x, z)

• Multiplying by 10m can be though of shifting the number by


m digits to the right. So it is not really a multiplication
7
D&Q Long Integer Multiplication
void Mult(char A[], char B[], char P[], int N){
if (N == 1){..; return; } // Base case

w = most significant n/2 digits of A;


x = least significant n/2 digits of A;
y = most significant n/2 digits of B;
z = most significant n/2 digits of B;

Mult(w, y, C, N/2); Shift C 2m digits to the right;


Mult(w, z, D, N/2); Shift D m digits to the right;
Mult(x, y, E, N/2); Shift E m digits to the right;
Mult(x, z, F, N/2);
P = C + D + E + F;
} //end-Mult

1 if n =1 (Base case)
T(n) = T(n) = (n2)
4*T(n/2) + n if n>1
Faster Long Integer Multiplication
n
n/2 n/2

w x A

y z B

• Let m = n/2, A = w*10m + x, B = y*10m + z

• C = Mult(w, y)
• D = Mult(x, z)
• E = Mult((w+x), (y+z)) - C - D;
• Product = C*102m + E*10m + D;

• Notice that we only have 3 multiplications here!


9
D&Q Long Integer Multiplication
void Mult(char A[], char B[], char P[], int N){
if (N == 1){..; return; } // Base case

w = most significant n/2 digits of A;


x = least significant n/2 digits of A;
y = most significant n/2 digits of B;
z = most significant n/2 digits of B;

Mult(w, y, C, N/2);
Mult(x, z, D, N/2);
Mult(w+x, y+z, E, N/2);
E -= C – D;
P = C*102m + E*10m + D;
} //end-Mult

1 if n =1 (Base case)
T(n) = T(n) = (n1.585)
3*T(n/2) + n if n>1
Summary of Long Int Multiplication
• Brute-force iterative algorithm = (n2)
• Divide & Conquer algorithm = (n1.585)

• But D&Q algorithm has a larger constant factor


hidden inside the asymptotic notation
– If we assume that D&Q algorithm’s overhead is 5
times more than the iterative algorithm, then D&Q
will be better for n2 >= 5*n1.585  n >= 50

– If the overhead is 10 times more, then D&Q will be


better for n >= 260

11

You might also like