Multiplication of large integers using Karatsuba and classical algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Multiplication of large integers using Karatsuba and classical algorithms

Description:

Multiplication of large integers using Karatsuba and classical algorithms Nghiep Tran Cs196M – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 13
Provided by: VoT9
Learn more at: http://www.cs.sjsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Multiplication of large integers using Karatsuba and classical algorithms


1
Multiplication of large integers using Karatsuba
and classical algorithms
  • Nghiep Tran
  • Cs196M

2
School multiplying
  • 2412
  • x 3231
  • -------
  • 2412
  • 7236
  • 4824
  • 7236
  • ---------------
  • 7793172

3
Karatsuba Multiplication (Divide-and-conquer
multiplication)
  • by Karatsuba and Ofman (1962)
  • aL aR aaL, aR ,b bL,
    bR
  • x bL bR aL left half of
    number a
  • ----------------- aR right half
    of number a
  • aLbR aRbR bL left half
    of number b
  • aLbL aRbL bR right half of number b
  • ---------------------------------
  • aLbL (aLbR aRbL) aRbR

4
Karatsuba Multiplication (Divide-and-conquer
multiplication)
  • ab aL bL 10n (aL bR aR bL) 10n/2 aR bR
  • x1 aL bL
  • x2 aR bR
  • x3 (aL aR) (bL bR)
  • ab x1 10n (x3 - x1 - x2) 10n/2 x2
  • aL bL 10n ((aL bL aL bR aR bL aR
    bR) - aL bL
  • - aRbR) 10n/2 aRbR
  • aLbL10n (aL bR aR bL) 10n/2 aR bR

5
Example
  • ab aL bL 10n (aL bR aR bL) 10n/2 aR bR
  • 1026 7329
  • aL 10, aR 26 bL 73, bR 29 n 4
  • 1026 7329
  • 1073104 (1029 2673)102 2629
  • 730104 2188 102 754
  • 7519554

6
School
  • Void school(int num1, int num2, int result,
    int len)
  • int i, j
  • for(i 0 i lt 2 len i)
  • resulti 0
  • for(i 0 i lt len i)
  • for(j 0 j lt len j)
  • resulti j num1i num2j

7
Karatsuba
  • BigInteger multiply(BigInteger a, BigInteger
    b)
  • int n max(number of digits in a, number of
    digits in b)
  • if(n 1)
  • return a b
  • else
  • BigInteger aR bottom n/2 digits of
    a
  • BigInteger aL top remaining digits
    of a
  • BigInteger bR bottom n/2 digits of
    b
  • BigInteger bL top remaining digits
    of b
  • BigInteger x1 Multiply(aL, bL)
  • BigInteger x2 Multiply(aR, bR)
  • BigInteger x3 Multiply(aL bL, aR
    bR)
  • return x1 pow(10, n) (x3 - x1 -
    x2) pow(10, n / 2) x2

8
Result
digits 8 16 32 64 128 256 512 1024 School 0.004423 0.011863 0.036895 0.127891 0.485230 1.831810 7.201439 28.628571 Karatsuba 0.004538 0.012674 0.036854 0.105385 0.317778 0.937266 2.846591 8.483051
9
Graph
10
Implementation
  • The karatsuba() will reverts to school() function
    when number of digits is less than CUTOFF_VALUE
    since karatsuba() is slower than school() for
    small values of n.
  • Carries are deferred until after the
    multiplication is complete.
  • Avoid allocating memory. This saves the overhead
    of continually allocating arrays.

11
Analysis
  • Grade school O(n2)
  • Karatsuba T(n) 3T(n/2) dn
  • O(nlg3)O(n1.585)
  • Split each of numbers into two halves each with
    n/2 digits.
  • Recursively multiply 4 pairs of n/2 digits
    numbers.-? O(n2) same as school
  • Clever way reduce the number of n/2 digit
    multiplication from 4 to 3.

12
Conclusion
  • Why would one want to multiply 100 digit numbers
    with exact precision?
  • Cryptography applications
  • RSA
  • Largest prime
  • FFT (Fast Fourier Transform) based algorithm -
    "Knuth algorithm"
  • Schonhage and Strassen in 1971
Write a Comment
User Comments (0)
About PowerShow.com