Download Math Topics - CS Course Webpages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Approximations of π wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Positional notation wikipedia , lookup

List of prime numbers wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Minimax wikipedia , lookup

Location arithmetic wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

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
kth coefficient is C(n,k) – i.e. n choose k – which is n!/[(n-k)!k!]
Notice C(n,k) = C(n,n-k)
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
• Can use a Dynamic Programming approach:
• C(n, k) = C(n-1,k-1) + C(n-1,k)
• Good if you need most but not 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
Combinatorics
• See the discussion earlier about binomial coefficients
• Choosing k objects from a set of n: C(n, k)
• Number of ways to permute objects = n!
• Note: factorial can only be computed to ~20!, and that’s with long longs
• So, must cancel out factorials when possible.
• Number of permutations on n objects where there are more than one
with same value: n!/[n1! n2! n3!...nk!]
• Choose k objects from n, but allow k to be chosen more than once
• C(n+k-1, k)
Combinatorics examples
• Given an nxm grid, how many rectangles can be made on the grid?
• Choose 2 vertical lines, choose 2 horizontal lines, then all combinations
• C(n,2)*C(m,2)
• Divide k balls into n boxes
• C(n+k-1, k)
• How many paths on a lattice from lower left to upper right, assuming
monotonic
• Use Dynamic Programming: 1 way to get to (0,0) and each (i,0) and (0, j)
• Then, paths to (i,j) = paths to (i-1, j) plus paths to (i, j-1)
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, xi = xi+kl
• Just let kl = i. SO, xi = x2i
• 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 kl = 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.