/
More Mathematical Techniques More Mathematical Techniques

More Mathematical Techniques - PowerPoint Presentation

jane-oiler
jane-oiler . @jane-oiler
Follow
342 views
Uploaded On 2019-11-21

More Mathematical Techniques - PPT Presentation

More Mathematical Techniques Special Number Sequences Fibonacci numbers Fib0 0 Fib1 1 Fibn Fibn1Fibn2 Dynamic Programming is usually used But there are faster methods matrix powers ID: 766391

gcd number prime numbers number gcd numbers prime row find fib cat pointer player start sequences lcm determine digits

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "More Mathematical Techniques" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

More Mathematical Techniques

Special Number Sequences Fibonacci numbers Fib(0) = 0, Fib(1) = 1, Fib(n) = Fib(n-1)+Fib(n-2) Dynamic Programming is usually used But, there are faster methods – matrix powers Sometimes require Java Bigints to use Zeckendorf’s Theorem: Every positive integer can be written as a sum of Fibonacci numbers, no two of which are consecutive. Can determine this greedily – choosing largest possible at each step Pisano Period Last digit/Last 2 digits/Last 3 digits/Last 4 digits repeat with period 60/300/1500/15000

Special Number Sequences Binomial Coefficients Coefficients of powers of a binomial ( x+y ) n k th coefficient is C( n,k ) Can use the ways to compute C, as mentioned earlier Can find with Pascal’s Triangle Row 1 is 1 Row 2 is 1 1 Row 3 is 1 2 1 Row 4 is 1 3 3 1 Each row is 1 longer. End values are 1, those in middle are sum of 2 above it Good if need all numbers

Special Number Sequences Catalan Numbers Cat(n) = C(2n,n)/(n+1) Cat(0) = 1 Cat(n+1) = Cat(n) * [(2n+2)(2n+1)]/[(n+2)(n+1)] Useful for several problems: Number of distinct binary trees with n vertices Number of expressions containing n pairs of parentheses that are correctly matched Number of ways n+1 factors can be parenthesized fully Number of ways a convex polygon with n+2 sides can be triangulated Number of monotonic paths on an nxn grid that don’t pass diagonal

Prime Numbers To test if a number is prime Could check divisibility by all numbers < sqrt(n) Could check divisibility by all ODD numbers < sqrt(n) Could check divisibility by all PRIME numbers < sqrt(n) To generate primes, could repeatedly test, then add on to list as new ones are found Useful if you just are testing one number, and it’s large. Or, use Sieve of Erastothenes to find all primes in some range Mark all numbers as primes to start; start pointer at 1 Repeat: Pick next prime number (on first iteration, this will be 2, then 3, then 5) Mark all multiples of that number as not prime (increment by that amount, marking not prime)

GCD and LCM GCD: gcd ( a,b ) Use Euclid’s algorithm If b == 0, return a, otherwise: return gcd (b, a%b ) Notice that gcd ( a,b,c ) = gcd ( a,gcd ( b,c )), etc. LCM: lcm( a,b ) calculate lcm( a,b ) = a*(b/ gcd ( a,b )) Notice you wan to divide by gcd first, to avoid intermediate overflow

Extended Euclid Algorithm Can tell x, y in a Diophantine equation: ax + by = c d = gcd ( a,b ). d must divide c First solution via extended Euclid algorithm Euclid( a,b ): If b == 0, x=1, y=0, d = a extendedEuclid (b, a%b ) x1 = y y1 = x – (a/b)*y x=x1 y=y1 That gives only the first (of an infinite number) valid solution, x0, y0 Others are x = x0 + (b/d) n; y =y0 + (a/d) n

Finding Repeating Cycles Sometimes you have a sequence in which the sequence begins repeating. Want to find point at which the repeating sequences start: This is m Want to find the length of the sequence. This is l Can store each value computed. Then, when new value is generated, see if you already found it. If you need the actual value when first found, store in a map. Value gives m , can subtract from current value to get l If you just need to know if a repetition exists, store in a set.

Floyd’s Cycle-Finding Notice that if l is the cycle length, then for i > m , x i = x i+k l Just let k l = i. SO, x i = x 2i Can iterate two pointers: a lower one that increases by 1 per step, and an upper one that increases by 2 per step When the two values match, you have i, 2i. So k l = i. To find m , start one pointer at i, and one at beginning. Then, increment them both by 1. When they match, the first is at m . Finally, you can get l by setting one pointer to m and one to m +1, then increment the bigger pointer until it matches m . That difference is l .

Game Playing Can determine who will win a (simple) game by exploring the game space. Use a minimax tree. Generate possible moves at each level Player 1 will pick the best from available choices (win if possible) Player 2 will pick the worst move from player 1’s perspective (i.e. make player 1 lose if possible) If each plays optimally, who will win? Can determine if you explore entire tree, or enough to ensure a choice is known at any level In real games (probably not contest), you can use alpha-beta pruning to speed things up.