Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Algorithms x x-1 Numerical Algorithms

Similar presentations


Presentation on theme: "Numerical Algorithms x x-1 Numerical Algorithms"— Presentation transcript:

1 Numerical Algorithms x 1 2 3 4 5 6 7 8 9 x-1 Numerical Algorithms
6/20/2018 9:29 AM Numerical Algorithms x 1 2 3 4 5 6 7 8 9 x-1 Numerical Algorithms

2 Outline Divisibility and primes Modular arithmetic
Euclid’s GCD algorithm Multiplicative inverses Powers Fermat’s little theorem Euler’s theorem Numerical Algorithms

3 Divisibility (1) Definition If a and b are integers with a ≠ 0, we say that a divides b if there is an integer c such that b = a∙c. When a divides b we say that a is a factor of b and that b is a multiple of a. The notation a|b denotes that a divides b. Numerical Algorithms

4 Divisibility (2) Theorem Let a, b, and c be integers (a, b, c  Z).
If a|b and a|c, then a|(b+c). If a|b, then a|b∙c for all integers c. If a|b and b|c, then a|c. Corollary If a, b, and c are integers (a, b, c  Z) such that a|b, and a|c, then a|m∙b+n∙c, for every pair of integers m and n. Theorem If a and b are positive integers and a|b, then a ≤ b. Note: 1|n for any integer n, and n|0 for any integer n ≠0. Numerical Algorithms

5 Fundamental Theorem of Arithmetic
Prime number p: p is an integer p  2 The only divisors of p are 1 and p Fundamental Theorem of Arithmetic The prime decomposition n = p1e1  …  pkek of a positive integer n is unique Examples: 200 = 23  52 100 = 2 ∙ 2 ∙ 5 ∙ 5 = 22 ∙ 52 641 = 641 999 = 3 ∙ 3 ∙ 3∙ 37 = 33 ∙ 37 1024 = 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 = 210 Numerical Algorithms

6 Greatest Common Divisor
The greatest common divisor (GCD) of two positive integers a and b, denoted gcd(a, b), is the largest positive integer that divides both a and b The above definition is extended to arbitrary integers Examples: gcd(18, 30) = 6 gcd(0, 20) = 20 gcd(-21, 49) = 7 Two integers a and b are said to be relatively prime if gcd(a, b) = 1 Example: Integers 15 and 28 are relatively prime Numerical Algorithms

7 Modular Arithmetic gcd(a, b) = gcd(b, a mod b)
Modulo operator for a positive integer n r = a mod n equivalent to a = r + kn and r = a - a/n n Example: 29 mod 13 = 3 13 mod 13 = 0 -1 mod 13 = 12 29 = 3 + 213 13 = 0 + 113 12 = 13 Modulo and GCD: gcd(a, b) = gcd(b, a mod b) gcd(21, 12) = 3 gcd(12, 21 mod 12) = gcd(12, 9) = 3 Numerical Algorithms

8 Euclid’s GCD Algorithm
Euclid’s algorithm for computing the GCD repeatedly applies the formula gcd(a, b) = gcd(b, a mod b) Example gcd(412, 260) = 4 Algorithm EuclidGCD(a, b) Input integers a and b Output gcd(a, b) if b = 0 return a else return EuclidGCD(b, a mod b) a 412 260 152 108 44 20 4 b Numerical Algorithms

9 Analysis Let ai and bi be the arguments of the i-th recursive call of algorithm EuclidGCD We have ai + 2 = bi + 1 = ai mod ai + 1 < ai + 1 Sequence a1, a2, …, an decreases exponentially, namely ai + 2  ½ ai for i > 1 Case 1 ai + 1  ½ ai ai + 2 < ai + 1  ½ ai Case 2 ai + 1 > ½ ai ai + 2 = ai mod ai + 1 = ai - ai + 1  ½ ai Thus, the maximum number of recursive calls of algorithm EuclidGCD(a. b) is 1 + 2 log max(a. b) Algorithm EuclidGCD(a, b) executes O(log max(a, b)) arithmetic operations Numerical Algorithms

10 Congruencies (1) Definition For integers a and n the expression a mod n is the reminder r of a when divided by n. In other words there is some integer q such that a = qn + r. Definition If a mod n = b mod n we say that a is congruent to b modulo n and we write a ≡ b mod n Numerical Algorithms

11 Congruencies (2) Theorem (a) If a ≡ x (mod n) and b ≡ y (mod n), then
a + b ≡ x + y (mod n), and a∙b ≡ x∙y (mod n). (b) If a∙c ≡ b∙c (mod n) and gcd(c, n) = 1, then a ≡ b (mod n). (c) If a ≡ b (mod n), then am ≡ bm (mod n) for all positive integers m. (d) If a ≡ b (mod m∙n), then a ≡ b (mod m) and (f) If a ≡ b (mod n), a ≡ b (mod m), and gcd(m, n) = 1, then a ≡ b (mod m∙n). Numerical Algorithms

12 Additive inverses The set of residues modulo a positive integer n is
Zn = {0, 1, 2, …, (n - 1)} Theorem Every element x in Zn has an additive inverse modulo n, i.e. there is a y in Zn such that (x + y) mod n = 0. Example: The additive inverse of 4 modulo 9 is 5. Numerical Algorithms

13 Multiplicative Inverses (1)
Let x and y be two elements of Zn such that xy mod n = 1 We say that y is the multiplicative inverse of x in Zn and we write y = x-1 Example: Multiplicative inverses of the residues modulo 11 x 1 2 3 4 5 6 7 8 9 10 x-1 Numerical Algorithms

14 Multiplicative Inverses (2)
Theorem An element x of Zn has a multiplicative inverse if and only if x and n are relatively prime Example The elements of Z10 with a multiplicative inverse are 1, 3, 7, 9 Corollary If p is prime, every nonzero residue in Zp has a multiplicative inverse A variation of Euclid’s GCD algorithm computes the multiplicative inverse of an element x of Zn or determines that it does not exist x 1 2 3 4 5 6 7 8 9 x-1 Numerical Algorithms

15 Powers Let p be a prime The sequences of successive powers of the elements of Zp exhibit repeating subsequences The sizes of the repeating subsequences and the number of their repetitions are the divisors of p - 1 Example (p = 7) x x2 x3 x4 x5 x6 1 2 4 3 6 5 Numerical Algorithms

16 Fermat’s Little Theorem
Let p be a prime. For each nonzero residue x of Zp, we have xp - 1 mod p = 1 Example (p = 5): 14 mod 5 = mod 5 = 16 mod 5 = 1 34 mod 5 = 81 mod 5 = mod 5 = 256 mod 5 = 1 Corollary Let p be a prime. For each nonzero residue x of Zp, the multiplicative inverse of x is xp - 2 mod p Proof x(xp - 2 mod p) mod p = xxp - 2 mod p = xp - 1 mod p = 1 Numerical Algorithms

17 Euler’s Theorem (1) The multiplicative group for Zn, denoted with Z*n, is the subset of elements of Zn relatively prime with n The totient function of n, denoted with f(n), is the size of Z*n Example Z*10 = { 1, 3, 7, 9 } f(10) = 4 If p is prime, we have Z*p = {1, 2, …, (p - 1)} f(p) = p - 1 Numerical Algorithms

18 Euler’s Theorem (2) Euler’s Theorem
For each element x of Z*n, we have xf(n) mod n = 1 Example (n = 10) 3f(10) mod 10 = 34 mod 10 = 81 mod 10 = 1 7f(10) mod 10 = 74 mod 10 = 2401 mod 10 = 1 9f(10) mod 10 = 94 mod 10 = 6561 mod 10 = 1 Numerical Algorithms

19 Modular Exponentiation (1)
The repeated squaring algorithm speeds up the computation of a modular power ap mod n Write the exponent p in binary p = pb - 1 pb - 2 … p1 p0 Start with Q1 = apb - 1 mod n Repeatedly compute Qi = ((Qi - 1)2 mod n)apb - i mod n We obtain Qb = ap mod n The repeated squaring algorithm performs O (log p) arithmetic operations Numerical Algorithms

20 Modular Exponentiation (2)
1 3p5 - i 3 Qi 9 5 18 Example 318 mod 19 (18 = 10010) Q1 = 31 mod 19 = 3 Q2 = (32 mod 19)30 mod 19 = 9 Q3 = (92 mod 19)30 mod 19 = 81 mod 19 = 5 Q4 = (52 mod 19)31 mod 19 = (25 mod 19)3 mod 19 = = 18 mod 19 = 18 Q5 = (182 mod 19)30 mod 19 = (324 mod 19) mod 19 = 17 mod 19 = 1 Numerical Algorithms

21 Modular Multiplicative Inverses (1)
Theorem Given positive integers a and b, let d be the smallest positive integer such that d = ia + jb for some integers i and j. We have d = gcd(a,b) Example a = 21 b = 15 d = 3 i = 3, j = -4 3 = 321 + (-4)15 = = 3 Numerical Algorithms

22 Modular Multiplicative Inverses (2)
Algorithm ExtendedEuclidGCD(a, b) Input: Nonnegative integers a and b Output: Triplet of integers (d, i, j) such that d=gcd(a, b)=ia+jb if b = 0 then return (a, 1, 0) q← a mod b Let r be the integer such that a = rb+q (d,k,l) ← ExtendedEuclidGCD(b,q) return (d, l, k-lr) Numerical Algorithms

23 Modular Multiplicative Inverses (3)
Theorem Given positive integers a and b, the extended Euclid’s algorithm for computing a triplet (d, i, j) such that d = gcd(a,b) d = ia + jb executes in O(log max(a, b)) arithmetic operations. Corollary Let x be an element of Zn such that gcd(x, n) = 1. The multiplicative inverse of x in Zn can be computed with O(log n) arithmetic operations. Numerical Algorithms

24 Modular Multiplicative Inverses (4)
To test the existence of and compute the inverse of x  Zn, execute the extended Euclid’s algorithm on the input pair (x,n) Let (d,i,j) be the triplet returned d = ix + jn Case 1: d = 1 i is the inverse of x in Zn Case 2: d > 1 x has no inverse in Zn Numerical Algorithms

25 Pseudoprimality Testing (1)
The number of primes less than or equal to n is about n / ln n Thus, we expect to find a prime among, O(b) randomly generated numbers with b bits each Testing whether a number is prime (primality testing) is believed to be a hard problem An integer n  2 is said to be a base-x pseudoprime if xn - 1 mod n = 1 (Fermat’s little theorem) Numerical Algorithms

26 Pseudoprimality Testing (2)
Composite base-x pseudoprimes are rare: A random 100-bit integer is a composite base-2 pseudoprime with probability less than 10-13 The smallest composite base-2 pseudoprime is 341 Base-x pseudoprimality testing for an integer n: Check whether xn - 1 mod n = 1 Can be performed efficiently with the repeated squaring algorithm Numerical Algorithms

27 Randomized Primality Testing
Compositeness witness function witness(x, n) with error probability q for a random variable x Case 1: n is prime witness w(x, n) = false Case 2: n is composite witness w(x, n) = false with probability q < 1 Algorithm RandPrimeTest tests whether n is prime by repeatedly evaluating witness(x, n) A variation of base- x pseudoprimality provides a suitable compositeness witness function for randomized primality testing (Rabin-Miller algorithm) Algorithm RandPrimeTest(n, k) Input integer n,confidence parameter k and composite witness function witness(x,n) with error probability q Output an indication of whether n is composite or prime with probability 2-k t  k/log2(1/q) for i  1 to t x  random() if witness(x,n)= true return “n is composite” return “n is prime” Numerical Algorithms


Download ppt "Numerical Algorithms x x-1 Numerical Algorithms"

Similar presentations


Ads by Google