Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing.

Similar presentations


Presentation on theme: "Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing."— Presentation transcript:

1 Mathematics in OI Prepared by Ivan Li

2 Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing

3 Greatest Common Divisor Motivation – Sometimes we want “k divides m” and “k divides n” occur simultaneously. – And we want to merge the two statements into one equivalent statement: “k divides ?”

4 Greatest Common Divisor Definition – The greatest natural number dividing both n and m – A natural number k dividing both n and m, such that for each natural number h dividing both n and m, we have k divisible by h.

5 Greatest Common Divisor How to find it? – Check each natural number not greater than m and n if it divides both m and n. Then select the greatest one. – Euclidean Algorithm

6 Euclidean Algorithm Assume m > n GCD(m,n) While n > 0 m = m mod n swap m and n Return m

7 Greatest Common Divisor What if we want the greatest number which divides n 1,n 2, …, n m-1 and n m ? Apply GCD two-by-two gcd(n 1,n 2, …,n m ) = gcd(n 1,gcd(n 2,gcd(n 3,…gcd(n m-1,n m )…))

8 Applications Solve mx + ny = a for integers x and y Can be solved if and only if a is divisible by gcd(m,n)

9 Applications Simplifying a fraction m/n If gcd(m,n) > 1, then the fraction can be simplified by dividing gcd(m,n) on the numerator and the denominator.

10 Least Common Multiple Definition – The least natural number divisible by both n and m – A natural number k divisible by both n and m, such that for each natural number h divisible by both n and m, we have k divides h. Formula – lcm(m,n) = mn/gcd(m,n)

11 Least Common Multiple What if we want to find the LCM of more than two numbers? Apply LCM two-by-two?

12 Definition of Prime Numbers An integer p greater than 1 such that: 1. p has factors 1 and p only? 2.If p = ab, a  b, then a = 1 and b = p ? 3.If p divides ab, then p divides a or p divides b ? 4. p divides (p - 1)! + 1 ?

13 Test for a prime number By Property 1 For each integer greater than 1 and less than p, check if it divides p Actually we need only to check integers not greater than sqrt(p) (Why?)

14 Finding Prime Numbers For each integer, check if it is a prime Prime List Sieve of Eratosthenes

15 Prime List Stores a list of prime numbers found For each integer, check if it is divisible by any of the prime numbers found If not, then it is a prime. Add it to the list.

16 Sieve of Eratosthenes Stores an array of Boolean values Comp[i] which indicates whether i is a known composite number

17 Sieve of Eratosthenes for i = 2 … n If not Comp[i] output i j = 2*i while j  n Comp[j] = true j = j + i

18 Optimization Consider odd numbers only Do not forget to add 2, the only even prime

19 High Precision Arithmetic 32-bit signed integer: -2147483648 … 2147483647 64-bit signed integer: -9223372036854775808 … 9223372036854775807 How to store a 100 digit number?

20 High Precision Arithmetic Use an array to store the digits of the number Operations: Comparison Addition / Subtraction Multiplication Division and remainder

21 High Precision Division Locate the position of the first digit of the quotient For each digit of the quotient (starting from the first digit), find its value by binary search.

22 High Precision Arithmetic How to select the base? Power of 2: Saves memory Power of 10: Easier input / output 1000 or 10000 for 16-bit integer array Beware of carry

23 More on HPA How to store negative numbers? fractions? floating-point numbers?

24 Partial Sum Motivation How to find the sum of the 3 rd to the 6 th element of an array a[i] ? a[3] + a[4] + a[5] + a[6] How to find the sum of the 1000 th to the 10000 th element? A for-loop will take much time In order to find the sum of a range in an array efficiently, we need to do some preprocessing.

25 Partial Sum Use an array s[i] to store the sum of the first i elements. s[i] = a[1] + a[2] + … + a[i] The sum of the j th element to the k th element = s[k] – s[j-1] We usually set s[0] = 0

26 Partial Sum How to compute s[i] ? During input s[0] = 0 for i = 1 to n input a[i] s[i] = s[i-1] + a[i]

27 Differencing Motivation How to increment the 3 rd to the 6 th element of an array a[i] ? a[3]++, a[4]++, a[5]++, a[6]++ How to increment the 1000 th to the 10000 th element? A for-loop will take much time In order to increment(or add an arbitrary value to) a range of elements in an array efficiently, we will use a special method to store the array.

28 Differencing Use an array d[i] to store the difference between a[i] and a[i- 1]. d[i] = a[i] - a[i-1] When the the j th element to the k th element is incremented, d[j] ++, d[k+1] - - We usually set d[1] = a[1]

29 Differencing Easy to compute d[i] But how to convert it back to a[i]? Before (or during) output a[0] = 0 for i = 1 to n a[i] = a[i-1] + d[i] output a[i] Quite similar to partial sum, isn’t it?

30 Relation between the two methods They are “inverse” of each other Denote the partial sum of a by  a Denote the difference of a by  a The difference operator We have  (  a) =  (  a) = a

31 Comparison Partial sum - Fast sum of range query Difference - Fast range incrementation Ordinary array - Fast query and incrementation on single element

32 Runtime Comparison Range Query Single Query Single Update Range Update Partial sumConstant (treat a single element as a range) Linear (Have to update all sums involved) Linear Ordinary array LinearConstant Linear DifferenceLinear (Convert it back to the original array) LinearConstant

33 Does there exist a method to perform range query and range update in constant time?

34 Question?


Download ppt "Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing."

Similar presentations


Ads by Google