Arithmetic and Algebra Daniel Butnaru daniel.butnaru@uni-konstanz.de 15. Dezember 2006 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 1/39
Outline 1 Introduction 2 Big Number Arithmetic 3 Modular Arithmetic 4 Linear Diophantine Equations 5 Gaussian Elimination Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 2/39
Outline 1 Introduction 2 Big Number Arithmetic 3 Modular Arithmetic 4 Linear Diophantine Equations 5 Gaussian Elimination Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 3/39
Definitions Arithmetic The mathematics of integers, rational numbers, real numbers, or complex numbers under addition, subtraction, multiplication, and division. Algebra A branch of mathematics in which symbols, usually letters of the alphabet, represent numbers or members of a specified set and are used to represent quantities and to express general relationships that hold for all members of the set. Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 4/39
Outline 1 Introduction 2 Big Number Arithmetic 3 Modular Arithmetic 4 Linear Diophantine Equations 5 Gaussian Elimination Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 5/39
Working with big numbers in computers Can be done by stringing digits together: Arrays of Digits the initial element of the array represents the least significant digit a counter with the length of the number in digits can aid efficiency Linked Lists of Digits necessary if we are really going to do arbitrary precision arithmetic Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 6/39
Example #define MAXDIGITS 100 #define PLUS 1 #define MINUS -1 typedef struct { char digits[maxdigits]; int signbit; int lastdigit; } bignum; Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 7/39
Example add_bignum(bignum *a, bignum *b, bignum *c) { int cr; int i; initialize_bignum(c); c->lastdigit = max(a->lastdigit,b->lastdigit)+1; cr = 0; for (i=0; i<=(c->lastdigit); i++) { c->digits[i] = (char) (cr + a->digits[i] + b->digits[i]) % 10; cr = (cr + a->digits[i] + b->digits[i]) / 10; } Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 8/39
Outline 1 Introduction 2 Big Number Arithmetic 3 Modular Arithmetic 4 Linear Diophantine Equations 5 Gaussian Elimination Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 9/39
Basics(1) a divides b when the equation b = k a with k Z is satisfied A number k which is divided only by 1 and itself is a prime number. Examples : 2, 3,5,7, 11... For a number a and some other number b there is exactly one q and one r with 0 r < n so that a = q b + r Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 10/39
Basics(2) A number k is called a common divisor for a and b if k a and k b. Among all the common divisors the biggest is called the greatest common divisor (GCD). Two numbers are relative prime if their greatest common divisor is 1. Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 11/39
Finding the GCD Euclid s Algorithm calculate gcd(a, b) after the division theorem we know a = q b + r 0 based on observing that gcd(a, b) = gcd(b, r 0 ) continue to apply the algorithm on (r k, r k+1 ) stop when the last remainder is 0 the gcd is the last non-zero remainder 1 a = q b + r 0 with 0 r 0 < b 2 b = q r 0 + r 1 with 0 r 1 < r 0 3 r 0 = q r 1 + r 2 with 0 r 2 < r 1 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 12/39
GCD(12, 5) Example 1 12 = 2 5 + 2 with 0 2 < 5 2 5 = 2 2 + 1 with 0 1 < 2 3 2 = 2 1 + 0 with 0 0 < 1 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 13/39
Euclid s Algorithm Recursive gcd(a, b) if b = 0 return a else return gcd(b, a mod b) Iterative gcd(a, b) while b!= 0 c := b b := a mod b a := c return a Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 14/39
Side Effects Bezout s Lemma Find k and l so that gcd(a, b) = a k + b l r 0 = a q 0 b r 1 = b q 1 r 0 r 2 = r 0 q 2 r 1 r 1 = b q 1 (a q 0 b) = k 1 a + l 1 b r 2 = (a q 0 b) q 2 (k 1 a + l 1 b) = k 2 a + l 2 b r 3 = (k 1 a + l 1 b) q 2 (k 2 a + l 2 b) = k 3 a + l 3 b Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 15/39
Bezut s Lemma Example 1 9 = 1 6 + 3 3 = 1 9 1 6 2 6 = 2 3 + 0 0 = 6 2 3 therefore, gcd(9, 6) = 9 1 6 1 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 16/39
Chinese Remainder Theorem We have a number n. Repeatedly divided by 3, the remainder is 2; by 5 the remainder is 3; and by 7 the remainder is 2. What number is n? Sun Tsu Suan-Ching (4th century AD) Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 17/39
Chinese Remainder Theorem Formal Given a set of congruences: x a i m i for i = 1,....r and for which the m i are pairwise relatively prime, the solution of the set of congruences is: x = a 1 b 1 M m 1 + + a r b r M m r Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 18/39
Chinese Remainder Theorem Formal Where, M = m 1 m 2 m r and the b i s can be obtained from solving the linear congruence equations: b i M m i 1 m i Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 19/39
Problem An old woman goes to market and a horse steps on her basket and crashes the eggs. The rider offers to pay for the damages and asks her how many eggs she had brought to sell. She does not remember the exact number, but when she had taken them out two at a time or 3 at a time, there was one egg left but when she took seven at a time they came out even. What is the smallest number of eggs she could have had? 1 x = 1 mod 2 2 x = 1 mod 3 3 x = 0 mod 7 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 20/39
Solution M = 2 3 7 = 42. Using the extended Euclid algorithm we can find solutions for the linear congruence equations: b 1 21 = 1 2 b 2 14 = 1 3 b 3 6 = 1 7 The/Some b i s that satisfy the above conditions are 1, 2 and -1. x = 1 1 21 + 1 2 14 + 0 1 21 = 49(eggs) Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 21/39
Outline 1 Introduction 2 Big Number Arithmetic 3 Modular Arithmetic 4 Linear Diophantine Equations 5 Gaussian Elimination Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 22/39
Linear Diophantine Equations Diophantus of Alexandria Hellenistic mathematician of the 3rd century AD, was one of the first mathematicians to introduce symbolism into algebra A Diophantine equation is an indeterminate polynomial equation that only allows the variables to be integers. with a i Z, b Z. a 1 x 1 + a 2 x 2 + + a n x n = b Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 23/39
Solving the Equations Condition A diophantine equation has a solution only when the greatest common divisor of all the a i s a divisor of b is and not all the a i are 0. Solution gcd(a 1, a 2,..., a n ) b with a i Z, b Z and (a 1, a 2,..., a n ) (0, 0,..., 0). Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 24/39
n = 2 Example Solve 114 x + 315 y = 6 with gcd(a 1, a 2 ) = 3 6 (solvable). 1 Divide the equation by 3. We get: 38 x + 105 y = 2 2 Use the extended Euclid algorithm to get the linear representation of 1 as a combination of 38 and 105: 47 38 17 105 = 1 3 The special solution is (47 2, 17 2) = (94, -34) 4 Set of all the solutions: {(94 + t 105, 34 t 38) t Z} Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 25/39
Solution for n = 2 Start from a 1 x 1 + a 2 x 2 = b with gcd(a 1, a 2 ) b and (a 1, a 2 ) (0, 0) 1 Divide the equation by gcd(a 1, a 2 ). We get a 1 x 1 + a 2 x 2 = b with gcd(a 1, a 2) = 1 2 Use the extended Euclid algorithm to get the linear representation of 1 as a combination of a 1 and a 2 : c 1 a 1 + c 2 a 2 = 1 3 a special solution for the diophantine equation is now (c 1 b, c 2 b) 4 The set of all the solutions can be obtained starting from our special solution: {(c 1 + t a 2, c 2 t a 1) t Z} Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 26/39
Solution for n > 2 Start from a 1 x 1 + a 2 x 2 + + a n x n = b with a i Z, b Z, (a 1, a 2,..., a n ) (0, 0,..., 0), gcd(a 1, a 2,..., a n ) = 1. 1 Rewrite the equation as follows: a 1 x 1 + a 2 x 2 + + a n 1 x n 1 = b a n x n 2 Consider x n constant. We have a diophantine equation with n 1 variables now. This has a solution only if: gcd(a 1, a 2,..., a n 1 ) (b a n x n ) 3 This is true only when there are c and c n so that: gcd(a 1, a 2,..., a n 1 ) c + a n c n = b 4 We solve this diophantine equation with two variables and we are down to n 1 variables in the initial equation. Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 27/39
n > 2 Example Solve 2 x + 4 y + 3 z = 3 with gcd(2, 3, 4) = 1 3 (solvable) 1 Rewrite the equation as follows: 2 x + 4 y = 3 3 z 2 This new diophantine equation with two variables is solvable only if gcd(2, 4) (3 3 z). By solving it we get the solution set: {( 3 + t 3, 3 t 2) t Z} 3 This gives us z = 3 t 2. After inserting this in the initial equation we are down to solving: 2 x + 4 y = 3 3 (3 2 t) x + 2 y = 3 + 3 t Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 28/39
n > 2 Example Solve x + 2 y = 3 + 3 t with gcd(1, 2) = 1 ( 3 + 3 t) (solvable) 1 The extended Euclid algorithm gives us ( 1) 1 + 1 2 = 1 and so: (3 3 t) 1 + ( 3 + 3t) 2 = 3 + 3 t 2 The solution set to this equation is: {((3 3 t) + s 2, ( 3 + 3t) s 1) s Z} 3 The final solution set: {((3 3 t) + s 2, ( 3 + 3t) s 1, 3 t 2) s, t Z} Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 29/39
Outline 1 Introduction 2 Big Number Arithmetic 3 Modular Arithmetic 4 Linear Diophantine Equations 5 Gaussian Elimination Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 30/39
Process Description Calculates solutions for linear equation systems Uses a matrix representation of the system Four elementary operations on the matrix a 11 x 1 + a 12 x 2 + + a 1n x n = b 1 a 21 x 1 + a 12 x 2 + + a 2n x n = b 2... a n1 x 1 + a n2 x 2 + + a nn x n = b n Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 31/39
Process Matrix Representation a 11 a 12... a 1n x 1 b 1 a 11 a 12... a 1n x 2...... = b 2... a 11 a 12... a 1n x n b n Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 32/39
Process Description Four elementary operations are applied on such equation systems without changing the solution: 1 switch equations rows in the matrix representation (also the solution vector) 2 rename variables equivalent to switching columns in the matrix 3 multiply a row with a constant ( 0) multiply a row in the matrix and solution vector with the same constant 4 add an equation to another equation add a row to another row in the matrix and solution vector. Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 33/39
Forward and Backward Elimination Aim Use the four operations on the matrix to bring it into the upper triangular form. r 11 r 12 r 13... r 1n x 1 c 1 0 r 21 r 23... r 2n x 2 c 2 0 0 r 33... r 3n 0 0...... 0 0...... r nn x 3... x n = As we use only equivalent transformations Rx = c will have the same solution as Ax = b : x i = 1 r ii (c i n k=i+1 r ik x k )(i = n 1, n 2,..., 1; x n = cn r nn ) c 3... c n Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 34/39
Process From A to R The first transformation, from A to A 1 : a 11 a 12... a 1n a11 1 a12 1... a1n 1 a 21 a 22... a 2n A = a 31 a 32... a 3n... A 0 a22 1... a 1 2n 1 = 0 a32 1... a 1 3n... a n1 a n2... a nn 0 an2 1... ann 1 1 find a pivot a r1 0 2 switch the 1 st and the r th rows 3 subtract from the rows i = 2,..., n the first row multiplied with a value l ij. a (1) ik = a ik l i1 a 1k, where l i1 = a i1 a 11, b (1) i = b i l i1 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 35/39
Gaussian Elimination Example 9 3 4 7 4 3 4 8 1 1 1 3 1 1 1 3 4 3 4 8 9 3 4 7 1 1 1 3 4 3 4 8 0-6 -5-20 1 1 1 3 0-1 0-4 0-6 -5-20 1 1 1 3 0-1 0-4 0 0-5 4 1 x 1 = 1/5 2 x 2 = 4 3 x 3 = 4/5 Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 36/39
Summary Working with really big numbers use array/list representation and operations on these structures Chinese Remainder Theorem gives us solutions to equations of type: x x i mod n i Diophantine Equations integer solutions to equations of type: a 1 x 1 + a 2 x 2 + + a n x n = b Gaussian Elimination solves linear equations systems Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 37/39
Resources Steven S. Skiena and Miguel A. Revilla Programming challenges: the programming contest training manual Uwe Schöning Algorithmik Math World http://mathworld.wolfram.com Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 38/39
Questions? Daniel Butnaru daniel.butnaru@uni-konstanz.de Arithmetic and Algebra 39/39