1. Trang chủ
  2. » Giáo án - Bài giảng

Discrrete mathematics for computer science arithmetic

15 72 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 15
Dung lượng 168,83 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Fast Multiplication and the Master Theorem on Divide and Conquer... How fast can we multiply?• Adding two n-bit numbers takes On operations • How many operations to multiply two n-bit nu

Trang 1

Arithmetic

Trang 2

I Fast Multiplication and the Master Theorem on Divide and Conquer

Trang 3

How fast can we multiply?

• Adding two n-bit numbers takes O(n) operations

• How many operations to multiply two n-bit numbers?

• Or two n-decimal-digit numbers

– Difference is a factor of log210 ≈ 3.32

but the individual operations are harder

Trang 4

Grade School Algorithm is Θ(n2)

• But answer is only O(n) bits: Can we do better?

Trang 5

A Divide and Conquer

Algorithm

• Suppose n is even, n = 2m

• To compute a∙b

• Write a = a1∙2m + a0, b = b1∙2m + b0, where

a1, a0, b1, b0 are m-bit numbers (numbers <

2m) – the first and last m bits of a and b

a∙b = a1b1∙22m + (a1b0+a0b1)∙2m + a0b0

= a1b1∙(22m+2m) + (a1-a0)(b0-b1)∙2m +

a0b0∙(2m+1)

Only 3 m-bit multiplications!!!

Trang 6

How Fast?

• T(1)=1

• T(n) = 3T(n/2) + cn

• But how to solve this?

Trang 7

Master Theorem on D+C

recurrences

• T(1) = 1

• T(n) = aT(n/b) + cn e

• Let L = logba

• Recurrence has the solution:

1 T(n) = Θ(n e ) if e > L

2 T(n) = Θ(n e log n) if e = L

3 T(n) = Θ(n L ) if e < L

• Binary search: a=1, b=2, e=0, L=0 [Case 2]

• Merge sort: a=2, b=2, e=1, L=1 [Case 2]

• Ordinary mult: a=4, b=2, e=1, L=2 [Case 3]

• Fast mult: a=3, b=2, e=1, L=lg 3 so Θ(n 1.58… ) [Case 3]

Trang 8

lec 4F.8

Compute 313:

313 = 3∙3∙3∙3∙3∙3∙3∙3∙3∙3∙3∙3∙3

(12 multiplications, or Θ(exponent))

313 = 36∙36∙3 (2 multiplications)

36 = 33∙33 (1 multiplication)

33 can be computed with 2

multiplications

So 2+1+2 = 5 multiplications in all!

II: Fast Exponentiation

Trang 9

lec 4F.9

compute ab using registers

X,Y,Z,R

X:= a ; Y:= 1; Z:= b ;

REPEAT:

if Z=0, then return Y

R:= remdr(Z,2); Z:= quotnt(Z,2)

if R=1,then Y:= X Y ⋅

X:= X 2

Fast Exponentiation

Trang 10

February 28, 2007 Harvard Bits 10

Powers by Repeated Squaring

• Problem: compute ab

• Method 1: multiply a by itself n-1 times

– Requires n-1 multiplications

• Method 2: use successive squaring

– How many times can you divide n by 2

before it is reduced to 1?

– Repeated squaring requires between

– Huge savings! n = 1000 => at most 20

multiplications! (since log21000 < 10)

Trang 11

February 28, 2007 11

III Modular arithmetic

1

2

3 4

5 6

7

0

6 + 5 = 3 (mod 8)

Trang 12

February 28, 2007 12

Math Quiz

2 x 6 = mod 11

2 x 6 x 5 = mod 11

23 = mod 7

2300 = mod 7

1

1

5

1

= (23)100 = 1100 = 1

Trang 13

February 28, 2007 Harvard Bits 13

(mod p) notation

• Think of the (mod p) at the end of

the line as referring to everything in the equation

• (23)100 = 1100 = 1 (mod 7) means

“(23)100 , 1100 , and 1 are all equivalent if

you divide by 7 and keep just the

remainder”

Often written a ≡ b (mod p)

Trang 14

February 28, 2007 Harvard Bits 14

Fast Modular Exponentiation

• Problem: Given q and p and n, find y < p such

that

q n = y (mod p)

• Method 1: multiply q by itself n-1 times

– Requires n-1 multiplications

• Method 2: use successive squaring

– Requires about log2n multiplications

• Same idea works for multiplication modulo p

• Example: If n is a 500-digit number, we can

compute q n (mod p) in about 1700 (= lg 10500 ) steps.

Trang 15

FINIS

Ngày đăng: 22/03/2019, 10:55

TỪ KHÓA LIÊN QUAN