Math Problems Francis Fok.

Slides:



Advertisements
Similar presentations
Euclidean Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson.
Advertisements

1 Lect. 12: Number Theory. Contents Prime and Relative Prime Numbers Modular Arithmetic Fermat’s and Euler’s Theorem Extended Euclid’s Algorithm.
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 ◦
5.1 Number Theory. The study of numbers and their properties. The numbers we use to count are called the Natural Numbers or Counting Numbers.
CSE 311 Foundations of Computing I Lecture 13 Number Theory Autumn 2012 CSE
Notation Intro. Number Theory Online Cryptography Course Dan Boneh
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Great Theoretical Ideas in Computer Science.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Slide 5-1 Copyright © 2005 Pearson Education, Inc. SEVENTH EDITION and EXPANDED SEVENTH EDITION.
Mathematics of Cryptography Part I: Modular Arithmetic
COMP 170 L2 Page 1 L05: Inverses and GCDs l Objective: n When does have an inverse? n How to compute the inverse? n Need: Greatest common dividers (GCDs)
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Function Recursion Examples “an example is worth a 1000 lectures”
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
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.
Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Basic Concepts in Number Theory Background for Random Number Generation 1.For any pair of integers n and m, m  0, there exists a unique pair of integers.
Prabhas Chongstitvatana1 Factorizing large integers Finding the unique decomposition of n into a product of prime factors. Factorize(n) if n is prime done.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 31.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Math 3121 Abstract Algebra I Lecture 9 Finish Section 10 Section 11.
Great Theoretical Ideas in Computer Science.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Math Problems Francis Fok 9 th Oct. Content Greatest common divisor Prime number algorithm Find power Other forms of integer.
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Notation Intro. Number Theory Online Cryptography Course Dan Boneh
Module #9 – Number Theory 1/5/ Algorithms, The Integers and Matrices.
Great Theoretical Ideas in Computer Science for Some.
Modular Arithmetic and the RSA Cryptosystem Great Theoretical Ideas In Computer Science John LaffertyCS Fall 2005 Lecture 9Sept 27, 2005Carnegie.
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.
9.1 Primes and Related Congruence Equations 23 Sep 2013.
Great Theoretical Ideas in Computer Science.
MA/CSSE 473 Day 07 Euclid's Algorithm. MA/CSSE 473 Day 07 Student Questions Review topics not covered in class Euclid's algorithm (if there is time) extended.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Number Theory: Prime and Composite Numbers
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Module #9 – Number Theory 6/11/20161 Chapter 3 Algorithms, Integers and Matrices.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
Great Theoretical Ideas in Computer Science.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 1 Integers Department of Computer Science and Information Engineering,
Revision. Cryptography depends on some properties of prime numbers. One of these is that it is rather easy to generate large prime numbers, but much harder.
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Mathematics Introduction & Formulas. Number System Number System is a writing system for expressing numbers.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
MATH301- DISCRETE MATHEMATICS Copyright © Nahid Sultana Dr. Nahid Sultana Chapter 4: Number Theory and Cryptography.
Number Theory and Modular Arithmetic
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Number Theory (Chapter 7)
Algorithm design and Analysis
CS 201 Fundamental Structures of Computer Science
Copyright © Zeph Grunschlag,
Presentation transcript:

Math Problems Francis Fok

Content Greatest common divisor Prime number algorithm Find power Basic Statistics Other forms of integer

Greatest common divisor Definition Divisibility : a | b if there exists an integer x, such that a * x = b We call a is divisor of b.

Definition Common divisor : c is divisor of both x and y if c | x and c | y. Greatest Common divisor: d = gcd(x,y) = Max { c | c is common divisor of x,y)

Properties gcd(x,y) ≥ 0 gcd(x,0) = x for x != 0 gcd(x,1) = 1 if y | x , then gcd(x,y) = y if x ≥ y > 0, then gcd(x,y) = gcd(y,x mod y) (it is known as “Euclidean algorithm”)

gcd (Recursive style) int gcd(int x, int y){ if(x % y == 0) return y; return gcd(y, x % y); } Assume x > 0 and y > 0 here “%” means “mod”

gcd (Iterative style) int gcd(int x,int y){ while(x % y != 0){ int t=x; x=y; y=t % y; } return y; Assume x > 0 and y > 0 here

lcm (least common multiple) Lemma : If x , y > 0, then x * y = gcd(x,y) * lcm(x,y) (Algorithm) int lcm(int x, int y){ return(abs(x) * abs(y) / gcd(x,y)); }

Extended Euclidean algorithm Problem definition Given a pair integer x, y (all are positive) Find a pair of integer a,b such that ax+by=gcd(x,y)

Extended Euclidean algorithm int gcd(int x, int y, int &a, int &b){ if(x mod y == 0){ a=0; b=1; return y; } int temp=a; a=b; b=temp – b * (temp div b); return gcd(y, x % y);

Prime number Definition: (General accepted) Prime number = { x | x have only 2 distinct positive divisor} Composite number = { x | x>1 and x is not prime} Normally, 1 is neither prime nor composite. Sometime , 1 will consider as a prime or a composite or both.

Example 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Example 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Example 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Example 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Example 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

Find Prime (Version 1) find_prime_1(int x){ array prime; //(may use vector) for(i=2 to x){ bool p=1; for(j = all index in prime) if(i%prime[j]==0) p=0; if(p) insert i into prime }

Algorithm (Version 1) Adv : Simple idea and simple coding space efficient Dis : Time inefficient Can we replace the “division” by “addition” and “mutliplication” only?

Find Prime (Version 2a) find_prime_2a(int x){ bool array prime[x]; initial all prime[i]=1; prime[0]=0; prime[1]=0; for(i = 2 to x) if(prime[i]) for(int j=i*i; j<=x; j+=i) prime[j]=0; }

Find Prime (Version 2b) find_prime_2b(int x){ bool array prime[x]; initial all prime[i]=1; prime[0]=0; prime[1]=0; prime[all even except 2] = 0; for(i = 3 ; i<=sqrt(x); i+=2){ if(prime[i]) for(int j=i*i; j<=x; j+=i) prime[j]=0; }

Find Prime (Version 2b) Adv : time efficient Dis : Only return “Yes” or “No”. Can we get the divisor from the algorithm?

Find Prime (Version 3) find_prime_3(int x){ int array prime[x]; initial all prime[i]=i; prime[0]=0; prime[1]=0; prime[all even] = 2; for(i = 3 ; i<=sqrt(x); i+=2){ if(prime[i]==i) for(int j=i*i; j<=x; j+=i) if(prime[j] = j) prime[j]=i; }

Example 2 3 5 6 7 8 9 10 11 12 13 15 16 17 18 19 20 21 22 23 25 26 27 28 29 30 31 32 33 35 36 37 38 39 40 41 42 43 45 46 47 48 49 50 51 52 53 55 56 57 58 59 60 61 62 63 65 66 67 68 69 70 71 72 73 75 76 77 78 79 80 81 82 83 85 86 87 88 89 90

Example 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 77 79 83 89

Number theory Definition: Relative prime x and y are relative prime iff gcd(x,y)=1 R(x) = { y | y<x and gcd(x,y)=1} Φ(x) = | R(x) |

Formula Φ(x) = x * product (1-1/p) where p is prime factor of x Example : Φ(6) = 6 * (1-1/2) * (1-1/3) = 2 Φ(24) = 24 * (1-1/2) * (1-1/3) = 8

phi phi(int x){ int sol=x,temp; while(x!=1){ temp=prime[x]; y=y*(temp-1)/temp; while(prime[x]==temp) x/=prime[x];} return sol; }

Find Power Problem: How to find 2^n? Algorithm(Version 1) find_power(int x){ if(x==1) return(2); return(2 * find(power(x-1)); } Problem : time inefficient (linear time) What happen if we call find_power(100000000)?

Find Power How we get 2^100000000 by hand? (It is a DP approach) 2^1=2 2^2=4 2^4=16 2^8=256 …… Find the binary representation of 100000000 and multiplying with corresponding power.

Find Power (Version 2) find_power(int x){ if(x==1) return(2) y = power(x/2) if(x mod 2==1) return(2 * y * y) else return(y * y) }

Find Power (Version 2) Adv : sub-linear time can be improved to do any base rather than 2 Even more powerful, the base can be a matrix.

Example Find

Fibonacci number It seem nothing special using a matrix as base. Question : Can we find the 1024th Fibonacci number in only 10 steps? (suppose no overflowing problem)

Fibonacci number Refer to Bryan’s presentation : Definition of Fibonacci number f(0) = 0 f(1) = 1 f(n) = f(n – 1) + f(n – 2), it requires 1024 steps to do. How we can only use 10 steps?

Fibonacci number We can construct a matrix representing the recurrence relation. Then, we can find the solution by

Sum of root power Suppose p,q are non-zero roots of x2 + bx + c = 0 where b,c are integers. Then, how to find pn + qn for some positive integer n?

Sum of root power Direct method : Find p and q by using the quadratic formula and power the value up to n. What is the problem?

Sum of root power We know, S = p + q = -b P = pq = c

Sum of root power Define F(n) = pn + qn . Then, F(n) = pn + qn = (pn-1 + qn-1)(p+q) – (pn-2 + qn-2) pq = S F(n-1) – P F(n-2) So,

Recurrence relation In fact, we can model any recurrence relation by a matrix. And find the nth term in sub-linear time.

Improvement If the base is a matrix , Using iterative style is better because of memory consideration. Applying Jordan decomposition , we can do the multiplication even more faster. But the drawback is the decomposition is very hard.

Other forms of integer The type “int”, “unsinged int”, “long long” have a range. int [- 2^31 , 2^31-1] unsigned int [ 0 , 2^32-1] long long [- 2^63 , 2^63-1] unsigned long long [ 0 , 2^64-1]

Use Double as integer Lemma : Any real number can be represented as Mantissa * 10^Exponent where 0≤Mantissa <1 And Exponent is an integer

Use Double as integer Question : How to find the last 4 digits of 2^1000? How to find the first 4 digits of 2^1000? How to find the number of digits of 2^1000? Of course, we can use Big-int to do. But we can do it faster.

Find last 4 digits Using the find_power and “mod”. find_last(int x){ if(x==1) return(2) y = power(x/2) mod 10000 if(x mod 2==1) return(2 * y * y) mod 10000 else return(y * y) mod 10000 }

Find first 4 digits Use mantissa to record the solution. find_first(int x){ mantissa=0.2; for(int i=1 to x){ mantissa *=2; if(mantissa >=1) mantissa /=10; } Becare : The answer = int(mantissa*10000)

Find the number of digits Use the exponent to record the solution. find_first(int x){ mantissa=0.2; expontent = 1; for(int i=1 to x){ mantissa *=2; if(mantissa >=1) mantissa /=10; ++expontent; } Becare : The answer = expontent

Find the number of digits There is another way to find the number of digit by using “log”. Number of digit = int(log 2^1000) + 1 = int(1000 log 2) + 1

Use Double as integer Using Double is taking a risk because of precision consideration. By the concept of numerical analysis, we can use double if we can control the error. Otherwise, using another method to do so.

Simple counting Combination and Permutation Use int or long long may suffer from overflow Use double may suffer from precision

DP approach By the identity, C(n,r) = C(n-1,r) + C(n-1,r-1) We can find a DP + Big Int solution.

Basic Statistics Given x1, x2, … , xn, (assume all distinct), we want to find Min or Max Find Min and Max Find Mean and variance Find Median(Do not talk this time)

Find Min or Max Just go through all Xi , record the current low(high). Number of steps = n Can you do that faster than “n”?

Find Min or Max If you require the correct answer, I can’t do faster than that number. If you accept the approximate, I may do it in O(lg n).

Find Min and Max Simple? Do findMin and findMax again. Number of steps = 2n. Can you do it faster?

Find Min and Max findMinMax(X[1…n]) left = MinMax(X[1…n/2]) right = MinMax(X[n/2+1…n]) return Min(Minright,MinLeft) Max(Minright,MinLeft) If you know how to count the number of steps, it is “n-1”.

Find Mean and variance mean = sum of Xi / n Var = sum of (Xi – mean)2 You can find both mean in O(n)-time.

Find Mean and variance But what happen if there is a new number Xn+1 What happen there is always a new number, Xn+2 , Xn+3 , … X2n The old style requires O(n2). Can you do it still in O(n).

Find Mean and variance When the number is being added. Take Sj = sum of Xj Update So, S1, S2 Then you can find mean and variance in O(1)-time.

Find Mean and variance Mean = sum of Xi / n = S1 / S0 Var = sum of (Xi – Mean)2 / n = sum of(Xi2) – Mean2 = S2 / S0 – (S1 / S0)2

Find Median Any Ideas?

Find Median Sorting and report the middle element. It requires O(n lg n). There is a O(n) algorithm to find the median.

Find Median What happen we want to find the k-smallest element? Again, linear-time selection algorithm can be used. (using wiki, “Selection algorithm”)

Selected Problem GCD: 10090,10104 Prime: 160,324,583,10139,10539,10650,10852 Number theory: 10090,10465,11064 Find power: 374,495,10655 Double as integer: 474,530,701,11029