We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Logarithm Quiz

Exponents grow very quickly, and logarithms grow very slowly. A logarithm is the inverse of an exponent.

Generally speaking, it's nice when we can write code that uses log(n) time to run, where n is the amount of data to process. For example, let's say we have a list of 1,000,000 users, and we want to write an algorithm that finds the user with the most followers.

If it takes 1 millisecond to check one user (let's just pretend), a linear algorithm would take 1,000,000 milliseconds, or about 16 minutes and 40 seconds.

A quadratic algorithm (exponent) would take 1,000,000,000,000 milliseconds, or about 31.7 years.

However, a logarithmic algorithm would only take 20 milliseconds! Here's a table to illustrate the difference:

Input SizeLinear (n*2)Quadratic (n^2)Log (log2(n))
1020 ms100 ms3 ms
100200 ms10,000 ms7 ms
1,0002,000 ms1,000,000 ms10 ms
10,00020,000 ms100,000,000 ms14 ms
100,000200,000 ms10,000,000,000 ms17 ms
1,000,0002,000,000 ms1,000,000,000,000 ms20 ms