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

Slides:



Advertisements
Similar presentations
Mathematics in OI Bill Kwok 20/3/2010 HKOI 2010 Intermediate Training Acknowledgement: References and slides are extracted from: 1. Math in OI, ,
Advertisements

Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
Thinking Mathematically
Copyright © 2013, 2010, and 2007, Pearson Education, Inc.
Number Theory and Cryptography
Factors, Fractions, and Exponents
UNIVERSITY OF MASSACHUSETTS Dept
Number Theory Divides: b divides a if there is a whole number q such that a = qb. Factor: b will be a factor of a. Divisor: b is also a divisor of a. Multiple:
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
division algorithm Before we study divisibility, we must remember the division algorithm. r dividend = (divisor ⋅ quotient) + remainder.
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
BY MISS FARAH ADIBAH ADNAN IMK
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Homework Review notes Complete Worksheet #2. Homework State whether the conditional sentence is true or false 1.If 1 = 0, then 1 = – 1 True F F T.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
1 Properties of Integers Objectives At the end of this unit, students should be able to: State the division algorithm Apply the division algorithm Find.
Section 1.1 Numbers and Their Properties.
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
Simple Data Type Representation and conversion of numbers
Mathematics of Cryptography Part I: Modular Arithmetic
3.4/3.5 The Integers and Division/ Primes and Greatest Common Divisors Let each of a and b be integers. We say that a divides b, in symbols a | b, provided.
MATH 224 – Discrete Mathematics
Mathematics of Cryptography Modular Arithmetic, Congruence,
Thinking Mathematically
7th Grade Pre-algebra Chapter 5 Notes 1.
2.1 Integers Natural (Counting) Numbers: 1, 2, 3, 4, …
Mathematics in OI Prepared by Ivan Li. Mathematics in OI A brief content Greatest Common Divisor Modular Arithmetic Finding Primes Floating Point Arithmetic.
Chapter 5 Rational Expressions Algebra II Notes Mr. Heil.
Number Theory.  A prime number is a natural number greater than 1 that has exactly two factors (or divisors), itself and 1.  Prime numbers less than.
35 Adding Fractions Add Estimate the sum x = = Find the least common denominator ~...(find the LCM of 8 and 5).. ~ 8:
Rational Numbers Mathematics- Grade 7.
Integer Conversion Between Decimal and Binary Bases Conversion of decimal to binary more complicated Task accomplished by –Repeated division of decimal.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
4-7 6th grade math Equivalent Fractions.
MM150 Unit 1 Seminar Agenda Welcome and Syllabus Review –Brief Syllabus Review –Contact Information for Instructor –Seminar Rules –Discussion Topics –Whole.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Foundations of Discrete Mathematics Chapter 4 By Dr. Dalia M. Gil, Ph.D.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
Module #9 – Number Theory 1/5/ Algorithms, The Integers and Matrices.
Introduction To Number Systems Binary System M. AL-Towaileb1.
Slide Copyright © 2009 Pearson Education, Inc. Unit 1 Number Theory MM-150 SURVEY OF MATHEMATICS – Jody Harris.
Greatest Common Divisors & Least Common Multiples  Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Integer Operations Computer Organization and Assembly Language: Module 5.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Ch04-Number Theory and Cryptography 1. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic.
FRACTIONS Fraction: a numerical quantity that is not a whole number Numerator: the number above the line in a common fraction showing how many of the parts.
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
Operations with Fractions. Parts of a Fraction Integer Numerator Denominator Mixed Number.
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Number Theory. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic principles of divisibility,
Preview Warm Up California Standards Lesson Presentation.
Mathematics of Cryptography
Number Theory and the Real Number System
CMSC Discrete Structures
CS/COE 0447 (term 2181) Jarrett Billingsley
MATH301- DISCRETE MATHEMATICS Copyright © Nahid Sultana Dr. Nahid Sultana Chapter 4: Number Theory and Cryptography.
Lecture 20 Guest lecturer: Neal Gupta
Enough Mathematical Appetizers!
Copyright © Zeph Grunschlag,
Introduction to Number Theory
UNIVERSITY OF MASSACHUSETTS Dept
UNIVERSITY OF MASSACHUSETTS Dept
Number Theory.
Cryptography Lecture 16.
Unit 1: Number System Fluency
Presentation transcript:

Mathematics in OI Prepared by Ivan Li

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

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 ?”

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.

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

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

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 )…))

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

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.

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)

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

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 ?

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?)

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

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.

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

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

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

High Precision Arithmetic 32-bit signed integer: … bit signed integer: … How to store a 100 digit number?

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

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.

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

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

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 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.

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

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]

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 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.

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]

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?

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

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

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

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

Question?