Math toolkit
Big-O and repeated trials
How log n, √N, n, n² and 2ⁿ grow, and how repeating a test drives its failure chance down
In the app every question checks your answer and explains a wrong one.
Big-O: growth without the constants
When you compare algorithms, what matters is how the cost grows as the input grows, not the exact count. Big-O notation says this briefly: means that grows no faster than , ignoring constant factors. Precisely, there is a constant with for every large enough .
For example, : once , , so . Constant factors and smaller terms drop out. Big-O is only an upper bound, so is true as well; it just says less than . When people quote a cost such as , they mean the smallest simple class that fits.
Check yourself
What is the smallest big-O class that contains , written in its simplest form?
- A
- B, since is the biggest term for small
- C
- D
Show the answer
Answer: C.
For , , so . No smaller power works, because the term eventually beats any constant times .
Why not the others
- A. That is the same class as , since . The simplest form drops the smaller term.
- B. Big-O is about large . Once , the term is larger than , and no constant times keeps up with .
- D. True, since grows no faster than , but far from the smallest class. for , so already holds.
Five growth rates
Five functions come up again and again in this course: , , , and . Each one eventually leaves the one before it far behind. Here they are at a few sizes, rounded:
Slow growers
Fast growers
Slow growers: on the scale of up to 100, hardly moves and only reaches 10. Fast growers: and stay close up to , where both are 16; after that leaves behind, and by it is off the chart.
Check yourself
From on, is larger than for a while. From which whole number on is larger than for good?
Hint
- Compare and at .
Show the answer
Answer:
, but . From there doubles at each step while grows by a factor , so stays ahead.
Common slips
- : Not yet: is still below .
- : At , catches , not : while .
Watch which variable a cost is measured in. Grover search, in Part 2, takes about steps to search items. If the items are labelled by -bit strings, then and
That is still exponential in the number of bits , only with half the exponent. A square-root speedup of an exponential cost leaves an exponential cost. It is a real gain, since is far smaller than , but it does not turn an exponential problem into a polynomial one.
Check yourself
A search space has items, and . What is ?
Show the answer
Answer:
, about a million, against , about a trillion. Still exponential in the number of bits.
Common slips
- : Taking a square root halves the exponent; it doesn’t subtract 2. .
- : That is . The square root applies to , which halves its exponent: .
Polynomial against exponential
A cost that grows like a power of the input size, such as , or , is called polynomial. A cost that grows like or is exponential. Polynomial-time algorithms count as efficient.
The difference shows when hardware improves. Suppose a computer becomes 1000 times faster. For an algorithm whose time grows like , the largest input you can handle in the same time grows by a factor . For one whose time grows like , it grows by : ten more bits, however large already was. No faster machine fixes an exponential cost, which is why turning an exponential cost into a polynomial one is the prize.
Check yourself
An algorithm takes time proportional to . A computer 1000 times faster arrives. By what factor does the largest input you can handle in the same time grow?
Hint
- If grows by a factor , by what factor does grow?
Show the answer
Answer:
Multiplying by multiplies by . The new machine does 1000 times the work in the same time, so and .
Common slips
- : The time per step falls by 1000, but the work grows like : multiplying by multiplies the work by . So .
- : That is , the factor for an algorithm. For take the cube root.
Check yourself
An algorithm takes time proportional to and handles in a day. On a computer 1000 times faster, about what can it handle in a day?
Hint
- Each extra bit doubles the running time. How many doublings make 1000?
Show the answer
Answer:
The new machine does 1000 times the work in a day, so the new satisfies , which gives .
Common slips
- : That would hold for an algorithm whose time grows like . With , each extra bit doubles the time, so 1000 times the speed buys only more bits.
- : That multiplies by , which is right for an algorithm. With , 1000 times the speed adds about 10 to .
Repeating a test
Many algorithms succeed only with some probability per run, but you can check whether a run succeeded, for example by testing the answer it gives. Then you repeat until a run succeeds. All of independent runs fail with probability , and since ,
To push the chance of failure below , it is enough that , that is . So about runs suffice. The dependence on is only logarithmic: going from a failure chance of one in a thousand to one in a million just doubles the number of runs, since .
Stop at a success (p = 1/4)
Majority vote (right 2/3 each run)
Stop at a success: a test that succeeds with probability per run. The chance that every run fails (bars) shrinks by a factor per run and stays under (dots). Majority vote: a vote over runs of an algorithm that is right of the time; the next part explains it.
Check yourself
Each run succeeds with probability , and you can tell when it has. Using , about how many runs make the chance that all of them fail at most ?
Hint
- .
Show the answer
Answer:
. The exact requirement gives , so the bound costs almost nothing.
Common slips
- : That is . After 100 runs all have failed with probability , far above . You need times as many.
- : That is . The number of runs depends on only through , and on through .
- : That multiplies by . The dependence on is logarithmic: multiply by instead.
Majority vote
Some algorithms give an answer that is right with probability at least , and you can’t check the answer directly. Run such an algorithm times, with odd, and take the majority answer. The majority is wrong only if more than half the runs are wrong, and that becomes unlikely fast. A result called the Chernoff bound shows that the chance falls off exponentially in , roughly like for a constant that depends on how far the success chance is above .
In the majority-vote chart above, three runs give a wrong majority with probability , and 21 runs with probability about . Any fixed success chance above can be pushed close to certainty with a modest number of runs. That is why “right with probability ” is the usual standard for a randomized algorithm: the itself is arbitrary.
Check yourself
An algorithm answers correctly with probability . You run it three times and take the majority answer. What is the probability that the majority is wrong?
Hints
- The majority is wrong when two or three of the runs are wrong.
- Exactly two wrong can happen in 3 ways, each with probability .
Show the answer
Answer:
Exactly two wrong: . All three wrong: . Together , already better than for one run.
Common slips
- : That is the chance that all three runs are wrong. The majority is also wrong when exactly two of the three are wrong: more.
- : That is the chance that exactly two runs are wrong. Add the case where all three are wrong, .
- : That is the chance for a single run. The majority needs at least two of the three runs to be wrong, which is less likely.
Go further
Free resources for when you want more than this refresher covers.
- Big O notationWikipedia · Article
The formal definition, its cousins Ω and Θ, and a table of common orders from constant time to factorial.
- Time complexityWikipedia · Article
A tour of running times from logarithmic to exponential, with a table of named classes and example algorithms.
- Chernoff boundWikipedia · Article
The precise bounds behind majority voting, including their use to boost the success chance of randomized algorithms.
Lessons that use it
- Lesson 11: Circuits as programsUniversal gate sets, controlled gates, and what a circuit costs