Addition
description
Transcript of Addition
![Page 1: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/1.jpg)
Addition
How fast can you add A+B
![Page 2: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/2.jpg)
Addition
How fast can you add A+B
![Page 3: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/3.jpg)
Addition
How fast can you add A+B
![Page 4: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/4.jpg)
Addition
How fast can you add A+B
![Page 5: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/5.jpg)
Addition
How fast can you add A+B
![Page 6: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/6.jpg)
Addition
n-bit numbers time = O(n)
How fast can you add A+B
![Page 7: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/7.jpg)
Multiplication
How fast can you multiply A*B
![Page 8: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/8.jpg)
Multiplication
How fast can you multiply A*B
![Page 9: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/9.jpg)
Multiplication
How fast can you multiply A*B
n-bit numbers time = O(n2)
![Page 10: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/10.jpg)
Karatsuba-Offman
a=2n/2 a1 + a0
b=2n/2 b1 + b0
ab=(2n/2a1+a0)(2n/2b1+b0) = 2n a1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0
![Page 11: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/11.jpg)
Karatsuba-Offman
a=2n/2 a1 + a0
b=2n/2 b1 + b0Multiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b1,n/2) R3 Multiply(a1,b0,n/2) R4 Multiply(a0,b0,n/2) return 2n R1+ 2n/2 (R2+R3) + R4
![Page 12: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/12.jpg)
Karatsuba-OffmanMultiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b1,n/2) R3 Multiply(a1,b0,n/2) R4 Multiply(a0,b0,n/2) return 2n R1+ 2n/2 (R2+R3) + R4
Recurrence?
![Page 13: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/13.jpg)
Karatsuba-OffmanMultiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b1,n/2) R3 Multiply(a1,b0,n/2) R4 Multiply(a0,b0,n/2) return 2n R1+ 2n/2 (R2+R3) + R4
Recurrence?
T(n) = 4T(n/2) + O(n)
![Page 14: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/14.jpg)
Karatsuba-Offman
T(n) = 4T(n/2) + O(n)
T(n)=O(n2)
![Page 15: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/15.jpg)
Karatsuba-Offmanab=(2n/2a1+a0)(2n/2b1+b0) = 2n a1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0
Can compute in less than 4 multiplications?
![Page 16: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/16.jpg)
Karatsuba-Offmanab=(2n/2a1+a0)(2n/2b1+b0) = 2n a1 b1 + 2n/2 (a1 b0 + a0 b1) + a0 b0
Can compute using 3 multiplications:
(a0+a1)(b0+b1) = a0b0 + (a1 b0 + a0 b1) + a1 b1
![Page 17: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/17.jpg)
Karatsuba-OffmanMultiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b0,n/2) R3 Multiply(a1+a0,b1+b0,n/2+1) R4 R3 – R2 – R1
return 2n R1+ 2n/2 R3 + R2
Recurrence?
![Page 18: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/18.jpg)
Karatsuba-OffmanMultiply(a,b,n) if n=1 return a*b else R1 Multiply(a1,b1,n/2) R2 Multiply(a0,b0,n/2) R3 Multiply(a1+a0,b1+b0,n/2+1) R4 R3 – R2 – R1
return 2n R1+ 2n/2 R3 + R2
Recurrence?
T(n) = 3T(n/2) + O(n)
![Page 19: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/19.jpg)
Karatsuba-Offman
T(n) = 3T(n/2) + O(n)
T(n)=O(nC)
C=log2 3 1.58
![Page 20: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/20.jpg)
Integer Division
a,b q,r
a = q*b + r0 r < b
Can be done in O(n2) time.
r=a mod b
![Page 21: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/21.jpg)
d divides a
DEFINITION: d divides a (denoted d | a) if there exists b such that b*d = a 3|63|0 0|30|0
![Page 22: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/22.jpg)
d divides a
DEFINITION: d divides a (denoted d | a) if there exists b such that b*d = a 3|6 yes, b=23|0 yes, b=0 0|3 no0|0 yes, b=?
![Page 23: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/23.jpg)
d divides a
3|6 yes, b=23|0 yes, b=0 0|3 no0|0 yes, b=?
d | a a | c d | c
Proof: a = b*d, c=b’*a c=(b*b’)*d
![Page 24: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/24.jpg)
Divisibility poset
1
2 3
4
5
6
7
8
9 10
0
![Page 25: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/25.jpg)
GCD
GCD (a,b) “largest” d such that d|a, d|b
![Page 26: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/26.jpg)
GCD
GCD (a,b) “largest” d such that d|a, d|b
d|a, d|b(c; c|a,c|b) : c|d
GCD(3,6)GCD(0,8)GCD(0,0)
![Page 27: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/27.jpg)
GCD
GCD (a,b) “largest” d such that d|a, d|b
d|a, d|b(c; c|a,c|b) : c|d
GCD(3,6) = 3GCD(0,8) = 8GCD(0,0) = 0
![Page 28: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/28.jpg)
GCD
How quickly can we computeGCD (a,b) ?
![Page 29: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/29.jpg)
GCD
How quickly can we computeGCD (a,b) ?
Euclid GCD(a,b) = GCD(b,a mod b)
![Page 30: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/30.jpg)
GCD
GCD(a,b) if b=0 then return a else return GCD(b,a mod b)
wlog a>b
Running time?
![Page 31: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/31.jpg)
GCD
GCD(a,b) if b=0 then return a else return GCD(b,a mod b)
wlog a>b
Running time?
(a,b)(b,a mod b)(a mod b, ?)(a mod b) < a/2
![Page 32: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/32.jpg)
GCD
(a,b)(b,a mod b)(a mod b, ?)(a mod b) < a/2
2(log2 a)=O(n) iterations
each mod O(n2) time O(n3) time total
![Page 33: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/33.jpg)
Modular exponentiation
(a,b,m) ab mod m
![Page 34: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/34.jpg)
Modular exponentiation
(a,b,m) ab mod m
a mod ma2 mod m a4 mod ma8 mod ma16 mod m...
b = 10101
ab mod m
![Page 35: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/35.jpg)
Modular exponentiation
(a,b,m) ab mod mmod-ex(a,b,m) if b=0 then RETURN 1 else if b mod 2 = 0 then RETURN mod-ex(a,b/2,m)2 mod m else RETURN a*mod-ex(a,(b-1)/2,m)2 mod m
![Page 36: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/36.jpg)
Algorithms so far
addition a+b O(n) timemultiplication a*b O(n1.58) timedivision a/b,a mod b O(n2) timegcd(a,b) O(n3) timeab mod m O(n3) time
a,b,m n-bit integers
![Page 37: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/37.jpg)
GROUP(G,) is a group if
GG G (ab)c = a(bc)exists G (aG) a = aa a-1
aa-1=
![Page 38: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/38.jpg)
Modular arithmetic modulo m
G = {0,...,m-1} = Zm
ab = a+b mod m
(G,) is a group if
GG G (ab)c = a(bc)exists G (aG) a = aa a-1
aa-1=
![Page 39: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/39.jpg)
Modular arithmetic modulo m
G = {0,...,m-1} = Zm
ab = a+b mod m
(G,) is a group if
GG G (ab)c = a(bc)exists G (aG) a = aa a-1
aa-1=
IS A GROUP
![Page 40: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/40.jpg)
Modular arithmetic modulo m
G = {0,...,m-1} = Zm
ab = a*b mod m
(G,) is a group if
GG G (ab)c = a(bc)exists G (aG) a = aa a-1
aa-1=
![Page 41: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/41.jpg)
Modular arithmetic modulo m
G = {0,...,m-1} = Zm
ab = a*b mod m
(G,) is a group if
GG G (ab)c = a(bc)exists G (aG) a = aa a-1
aa-1=
b; ab=1 [mod m]
GCD(a,m)=1
![Page 42: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/42.jpg)
Modular arithmetic modulo m
G = Z*m ={a | GCD(a,m)=1 }
ab = a*b mod m
(G,) is a group if
GG G (ab)c = a(bc)exists G (aG) a = aa a-1
aa-1=
IS A GROUP
![Page 43: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/43.jpg)
Fermat’s little Theorem
ap-1 = 1 [mod p]
p a prime
{ak | k Z} is a subgroup of Z*p
![Page 44: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/44.jpg)
Fermat’s little Theorem
m) = | Z*m |
a(m)=1 [mod m]
m=p1a1 p2
a2 ... pkak
m) = (1-1/p1) ... (1-1/pk) m
![Page 45: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/45.jpg)
Fermat’s little Theorem
m=p1a1 p2
a2 ... pkak
m) = (1-1/p1) ... (1-1/pk) m
E.g. if m=pq p,q primes (m)=
![Page 46: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/46.jpg)
Fermat’s little Theorem
m=p1a1 p2
a2 ... pkak
m) = (1-1/p1) ... (1-1/pk) m
E.g. if m=pq p,q primes (m)=(p-1)(q-1)
![Page 47: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/47.jpg)
Fermat’s little Theorem
E.g. if m=pq p,q primes (m)=(p-1)(q-1)
a(p-1)(q-1) =1 [mod pq]
![Page 48: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/48.jpg)
RSA1) choose primes p,q2) let n pq3) choose e4) compute d=e-1 [mod (p-1)(q-1)]5) announce n,e
![Page 49: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/49.jpg)
RSA1) choose primes p=13,q=172) let n pq3) choose e4) compute d=e-1 [mod (p-1)(q-1)]5) announce n,e
![Page 50: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/50.jpg)
RSA1) choose primes p=13,q=172) let n pq=2213) choose e4) compute d=e-1 [mod (p-1)(q-1)]5) announce n,e
![Page 51: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/51.jpg)
RSA1) choose primes p=13,q=172) let n pq=2213) choose e=54) compute d=e-1 [mod (p-1)(q-1)]5) announce n,e
![Page 52: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/52.jpg)
RSA1) choose primes p=13,q=172) let n pq=2213) choose e=54) compute 77=d=e-1 [mod (p-1)(q-1)]5) announce n,e
![Page 53: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/53.jpg)
RSA1) choose primes p=13,q=172) let n pq=2213) choose e=54) compute 77=d=e-1 [mod (p-1)(q-1)]5) announce n,e
n=221e=5
d = 77
![Page 54: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/54.jpg)
RSA
n=221e=5
ENCODE: x xe mod nDECODE: x xd mod n
d = 77
![Page 55: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/55.jpg)
n=221e=5
d = 77RSA
m=42
ENCODE: x xe mod nDECODE: x xd mod n
![Page 56: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/56.jpg)
n=221e=5
d = 77RSA
m=42
9
ENCODE: x xe mod nDECODE: x xd mod n
425 (mod 221) = 9
![Page 57: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/57.jpg)
n=221e=5
d = 77RSA
m=42
9
ENCODE: x xe mod nDECODE: x xd mod n
425 (mod 221) = 9 977 (mod 221) = 42
m=42
![Page 58: Addition](https://reader036.fdocuments.in/reader036/viewer/2022070404/56813b9c550346895da4d189/html5/thumbnails/58.jpg)
Primality testing