DSA Factory
All free lessons
Level 1 · Foundations

Free Math and bits lessons

16 lessons, about 131 minutes in all. Each teaches one idea with a visual, then gives you one problem built for it. Read the lessons here, then create a free account to solve the problems and keep your progress.

Stage 0Digits and divisibility

  1. Peel off the last digitWalk through a number's digits with % 10 and // 10.6 min
  2. Build a number digit by digitReverse a number by pushing digits onto the end of a new one.7 min
  3. Divisors come in pairsCount divisors by checking only up to the square root.9 min
  4. Euclid's shortcutFind the greatest common divisor by taking remainders.8 min

Stage 1Primes and remainders

  1. Cross out the multiplesFind every prime below n at once with the sieve of Eratosthenes.10 min
  2. Square and halveCompute a huge power's remainder quickly by squaring.10 min
  3. Count the fivesCount the zeros at the end of n! without computing it.8 min
  4. Remainder digit by digitFind the remainder of a number too long to store, reading it as text.8 min

Stage 2Bits

  1. Read the last bit, shift it awayCount the 1s in a number's binary form with & 1 and >> 1.8 min
  2. One bit onTest for a power of two in one step with n & (n − 1).7 min
  3. Pairs cancel outFind the one value without a partner by XOR-ing everything together.8 min
  4. Cancel against what should be thereFind the missing number from 0 to n by XOR-ing the list with the full range.7 min

Stage 3Bit tricks

  1. The lowest 1 bitIsolate a number's lowest 1 bit with n & −n.7 min
  2. Set, clear, flipChange single bits of a number with masks.9 min
  3. Every subset is a numberWalk all subsets of a small list by counting from 0 to 2ⁿ − 1.10 min
  4. Mirror the bitsReverse the 32 bits of a number.9 min

There’s more after this

These are the opening stages of Math and bits. The full path climbs on to harder problems and a mastery test. Create a free account and we’ll keep your place.

Start the full path free