Ch 6: Math and Logic Puzzles Flashcards

1
Q

Common Math Puzzle Topics

A
  • Prime Numbers
  • Probability Calculation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Prime Numbers:
Essential Concepts

A
  • Prime Factorization (Prime Number Law)
  • Divisibility
  • Greatest Common Denominator
  • Least Common Multiple
  • Multiplying GCD and LCM
  • Checking for Primality
  • Sieve of Eratosthenes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Prime Numbers:
Prime Factorization Law

A

Every positive integer can be decomposed into a product of primes

Examples:
3 = 2^0 * 3^1
10 = 2^1 * 3^0 * 5^1
84 = 2^2 * 3^1 * 5^0 * 7^1 * 11^0 * 13^0 * …..

Prime numbers in the factorization that are NOT factors have an exponent of 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Prime Numbers:
Divisibility of Integers:
Is a number Y divisible by X?

A

Because all integers are products of prime factors,
for a number X to divide a number Y,
All primes in X’s factorization must be present in Y’s factorization.
Y must include all of the prime factors of X.

Formally:
X = 2^j0 * 3^j1 * 5^j2 * 7^j3 * ….
Y = 2^k0 * 3^k1 * 5^k2 * 7^k3 * ….

If X divides Y, then for all i, ji <= ki

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Prime Numbers:
Greatest Common Divisor of two Integers

A

For each prime factor, use the smaller of the exponent between the two numbers:

Formally:
X = 2^j0 * 3^j1 * 5^j2 * 7^j3 * ….
Y = 2^k0 * 3^k1 * 5^k2 * 7^k3 * ….

GCD(X,Y) = 2^min(j0,k0) * 3^min(j1,k1) * 5^min(j2,k2) * …. * p^min(ji, ki)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Prime Numbers:
Least Common Multiple of Two Integers

A

For each prime factor, use the larger of the exponents between the two numbers:

Formally:
X = 2^j0 * 3^j1 * 5^j2 * 7^j3 * ….
Y = 2^k0 * 3^k1 * 5^k2 * 7^k3 * ….

LCM(X,Y) = 2^max(j0,k0) * 3^max(j1,k1) * 5^max(j2,k2) * …. * p^max(ji, ki)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Prime Numbers:
Three Common Ways to
Check if a Number is Prime

A
  • Naive: Brute Force
    Iterate from 2 to n-1,
    Checking if n is divisible by the number
    O(n)
  • Improved Naive:
    Same, but only check through sqrt(n)
    Everything larger is redundant
    O(sqrt(n) )
  • Sieve of Eratosthenes
    Efficiently generates list of numbers 2 to n
    If number is in the list, it is prime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Prime Numbers:
Sieve of Eratosthenes

A

*A highly efficient way to generate a list of prime numbers
* Core concept: all non-prime numbers are divisible by a prime number

Steps:
* Start with a list of numbers up through some value MAX
* Remove all numbers divisible by 2 (except 2)
Note: Can start here for increased efficiency
* Move to the next remaining number, this is the next prime
* Remove all numbers divisible by this prime
* Repeat until reaching sqrt(max)
Note: all non-primes larger than sqrt(max) will have already been
removed at this point
* The list now includes all primes from 2 to MAX

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Probability:
Important Concepts

A
  • Venn Diagram
  • Bayes Theorem
  • Probability of Events A AND B
  • Probability of Event A OR B
  • Independence
  • Mutually Exclusive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Probability:
Bayes’ Theorem

A

The probability of event A given event B,
is equal to the probability of event B given event A
multiplied by the ratio of the independent probabilities.

P(A given B) = P(B given A) * P(A) / P(B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Probability of A AND B

A

P(A and B) = P(B given A) * P(A)

alternatively,

P(A and B) = P(A given B) * P(B)

Together,
P(A given B)P(B) = P(B given A)P(A)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Probability of A OR B

A

Add the probability of P(A) and P(B).
This includes the intersection probability (A and B) twice,
so subtract it once

P(A or B) = P(A) + P(B) - P(A and B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Probability:
Independence

A

Two events are independent if the probability of one happening doesn’t tell you anything about the probability of the other happening.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Probability:
Probability of A AND B when
A and B are independent

A

P(A and B) = P(A) * P(B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Probability:
Mutual Exclusivity

A

If A and B are mutually exclusive, it means that if one happens, the other cannot happen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Probability:
Probability of A and B
when A and B are mutually exlusive

A

P(A and B) = 0,
because they are mutually exclusive, it is by definition impossible
for both to happen.

17
Q

Probability:
Probability of A or B
when A and B are mutually exclusive

A

P(A or B) = P(A) + P(B)

Note: when A and B are the only options,
this should add up to 100%

18
Q

Probability:
When are two events both independent
and mutually exclusive

A

Never.
If the events are mutually exclusive, then one of the events happening
changes the probability of the other event happening(it becomes 0)

Because the probability of mutually exclusive events depends on each other,
they are by definition NOT independent.

19
Q

Steps for approaching “Puzzle” or “Brainteaser” problems

A
  1. Start talking
    * Show the interviewer how you approach a problem
  2. Develop Rules and Patterns
    * As you work through the problem, write down rules or patterns that
    you discover or remember.
  3. Wost Case Shifting
    * Many brainteasers are worst-case minimization problems
    * Minimize an action or do something AT MOST X times
    * Try to “balance” the worst case:
    * If an early decision skews the worst case, change decision to make
    it better.
  4. Use Algorithmic Approaches
    * If stuck, use approaches for developing algorithms
    * Brainteasers are often algorithm questions with technical aspects
    removed
20
Q

Problem Solving:
2 Especially useful
Algorithm Strategies
for Brainteaser problems

A
  • Base Case
  • Do It Yourself