cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications
casting out 3s Theorem: A positive integer n is divisible by 3 if and only if the sum of its decimal digits is divisible by 3.
leaked game of thrones
pattern matching
pattern matching
basic applications of mod Hashing Pseudo random number generation Simple cipher
hashing Scenario: Map a small number of data values from a large domain 0, 1,, M 1 into a small set of locations 0,1,, n 1 so one can quickly check if some value is present.
hashing Scenario: Map a small number of data values from a large domain 0, 1,, M 1 into a small set of locations 0,1,, n 1 so one can quickly check if some value is present hash x = x mod p for p a prime close to n or hash x = (ax + b) mod p Depends on all of the bits of the data helps avoid collisions due to similar values need to manage them if they occur
Linear Congruential method: pseudo-random number generation x n+1 = a x n + c mod m Choose random x 0, a, c, m and produce a long sequence of x n s [good for some applications, really bad for many others]
simple ciphers Caesar cipher, A = 1, B = 2,... HELLO WORLD Shift cipher f(p) = (p + k) mod 26 f 1 (p) = (p k) mod 26 More general f(p) = (ap + b) mod 26
modular exponentiation mod 7 X 1 2 3 4 5 6 1 2 3 4 5 6 a a 1 a 2 a 3 a 4 a 5 a 6 1 2 3 4 5 6
modular exponentiation mod 7 X 1 2 3 4 5 6 1 1 2 3 4 5 6 2 2 4 6 1 3 5 3 3 6 2 5 1 4 4 4 1 5 2 6 3 5 5 3 1 6 4 2 6 6 5 4 3 2 1 a a 1 a 2 a 3 a 4 a 5 a 6 1 2 3 4 5 6
modular exponentiation mod 7 X 1 2 3 4 5 6 1 1 2 3 4 5 6 2 2 4 6 1 3 5 3 3 6 2 5 1 4 4 4 1 5 2 6 3 5 5 3 1 6 4 2 6 6 5 4 3 2 1 a a 1 a 2 a 3 a 4 a 5 a 6 1 1 1 1 1 1 1 2 2 4 1 2 4 1 3 3 2 6 4 5 1 4 4 2 1 4 2 1 5 5 4 6 2 3 1 6 6 1 6 1 6 1
exponentiation Compute 78365 81453 Compute 78365 81453 mod 104729 Output is small need to keep intermediate results small
repeated squaring small and fast Since a mod m a (mod m) for any a we have a 2 mod m = (a mod m) 2 mod m and a 4 mod m = (a 2 mod m) 2 mod m and a 8 mod m = (a 4 mod m) 2 mod m and a 16 mod m = (a 8 mod m) 2 mod m and a 32 mod m = (a 16 mod m) 2 mod m Can compute a k mod m for k = 2 i in only i steps
fast exponentiaion public static long FastModExp(long base, long exponent, long modulus) { long result = 1; base = base % modulus; } while (exponent > 0) { if ((exponent % 2) == 1) { result = (result * base) % modulus; exponent -= 1; } /* Note that exponent is definitely divisible by 2 here. */ exponent /= 2; base = (base * base) % modulus; /* The last iteration of the loop will always be exponent = 1 */ /* so, result will always be correct. */ } return result; b e mod m = (b 2 ) e/2 mod m, when e is even) b e mod m = (b*(b e-1 mod m) mod m)) mod m
Let M = 104729 program trace 78365 81453 mod M = ((78365 mod M) * (78365 81452 mod M)) mod M = (78365 * ((78365 2 mod M) 81452/2 mod M)) mod M = (78365 * ((78852) 40726 mod M)) mod M = (78365 * ((78852 2 mod M) 20363 mod M)) mod M = (78365 * (86632 20363 mod M)) mod M = (78365 * ((86632 mod M)* (86632 20362 mod M)) mod M =... = 45235
fast exponentiation algorithm Another way: 81453 = 2 16 + 2 13 + 2 12 + 2 11 + 2 10 + 2 9 + 2 5 + 2 3 + 2 2 + 2 0 a 81453 = a 216 a 213 a 212 a 211 a 210 a 29 a 25 a 23 a 22 a 20 a 81453 mod m= ( (((((a 216 mod m a 213 mod m ) mod m a 212 mod m) mod m a 211 mod m) mod m a 210 mod m) mod m a 29 mod m) mod m a 25 mod m) mod m a 23 mod m) mod m a 22 mod m) mod m a 20 mod m) mod m The fast exponentiation algorithm computes a n mod m using O(log n) multiplications mod m
primality An integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite.
fundamental theorem of arithmetic Every positive integer greater than 1 has a unique prime factorization 48 = 2 2 2 2 3 591 = 3 197 45,523 = 45,523 321,950 = 2 5 5 47 137 1,234,567,890 = 2 3 3 5 3,607 3,803
factorization If n is composite, it has a factor of size at most n.
euclid s theorem There are an infinite number of primes. Proof by contradiction: Suppose that there are only a finite number of primes: p 1, p 2,, p n
famous algorithmic problems Primality Testing Given an integer n, determine if n is prime Factoring Given an integer n, determine the prime factorization of n
factoring Factor the following 232 digit number [RSA768]: 12301866845301177551304949583849627207 72853569595334792197322452151726400507 26365751874520219978646938995647494277 40638459251925573263034537315482685079 17026122142913461670429214311602221240 47927473779408066535141959745985690214 3413
1230186684530117755130494958384962720772853569595334 7921973224521517264005072636575187452021997864693899 5647494277406384592519255732630345373154826850791702 6122142913461670429214311602221240479274737794080665 351419597459856902143413 334780716989568987860441698482126908177047949837 137685689124313889828837938780022876147116525317 43087737814467999489 367460436667995904282446337996279526322791581643 430876426760322838157396665112792333734171433968 10270092798736308917
GCD(a, b): greatest common divisor Largest integer d such that d a and d b GCD(100, 125) = GCD(17, 49) = GCD(11, 66) = GCD(13, 0) = GCD(180, 252) =
gcd and factoring a = 2 3 3 5 2 7 11 = 46,200 b = 2 3 2 5 3 7 13 = 204,750 GCD(a, b) = 2 min(3,1) 3 min(1,2) 5 min(2,3) 7 min(1,1) 11 min(1,0) 13 min(0,1) Factoring is expensive! Can we compute GCD(a,b) without factoring?
If a and b are positive integers, then gcd a, b = gcd(b, a mod b) useful GCD fact Proof: By definition a = a div b b + (a mod b) If d a and d b then d a mod b. If d b and d a mod b then d a.
Repeatedly use the GCD fact to reduce numbers until you get GCD x, 0 = x. euclid s algorithm GCD(660,126)
euclid s algorithm GCD(x, y) = GCD(y, x mod y) int GCD(int a, int b){ /* a >= b, b > 0 */ int tmp; while (b > 0) { tmp = a % b; a = b; b = tmp; } return a; } Example: GCD(660, 126)