Learn / How to think

Maths basics

The few arithmetic moves that most small number problems are built from: peel a digit, test a remainder, stop at the square root, and let gcd shrink the numbers.

How to recognise it

  • The question is about the digits of a number, not the number as a whole (count them, sum them, reverse them).
  • The input is a single integer and there is no array or string to walk through.
  • It asks for divisors, factors, primes, gcd, lcm or a factorial.
  • The number can be as large as 10^9, so looping from 1 up to n would take too long.

The idea in one picture

Think of a number as a stack of digit cards laid left to right. You can only touch the top card of the stack on the right-hand end. Two operations are all you get:

  • n % 10 looks at that last card and tells you the digit.
  • n / 10, with the remainder thrown away, removes that card.

Almost every digit problem is "look at the last card, do something with it, remove it, repeat until nothing is left".

Where the pattern shows up

Most problems here fall into four groups, and each has its own short move:

  1. Digit problems (count, sum, reverse, palindrome, Armstrong): peel digits with % 10 and / 10.
  2. Divisibility problems (perfect number, divisors): n % d == 0 says that d divides n.
  3. Prime and divisor search: factors come in pairs, so you only need to look up to the square root of n.
  4. gcd, lcm and powers: gcd shrinks two numbers quickly, lcm comes from gcd, and powers can be built by squaring.

Digit loop template

while n > 0:
    digit = n mod 10
    # do something with digit
    n = n div 10

Everything in group 1 is this loop with a different line in the middle.

A guided example: sum of the digits of 4729

Before reading the loop, ask what you would keep track of. You need a running total, so start it at 0.

nlast digittotal after adding it
472999
472211
47718
4422
0stop22

The only line that changed from the template is total = total + digit. Summing the digits of 4729 gives 22.

Stopping at the square root

If n = a * b, then a and b can't both be bigger than the square root of n, or their product would be bigger than n. So to find the divisors of n or to test it for primality, try i = 1, 2, 3, ... while i * i <= n. For n = 10^9 that is about 31,600 rounds instead of a billion. Each i that divides n also gives you its partner n / i for free.

gcd and lcm

Euclid's method: gcd(a, b) equals gcd(b, a % b), and gcd(a, 0) is a. The pair shrinks very fast, so the loop is short even for large numbers. The lcm follows from lcm(a, b) = a / gcd(a, b) * b. Divide first and multiply second, so the intermediate value stays small.

Common mistakes

  • Treating 0 as having no digits. A digit loop that runs while n > 0 does nothing for 0, so add a special case when the answer for 0 is not 0.
  • Overflow. Factorials grow past a 32-bit integer at 13!, and a * b in an lcm can overflow before you divide.
  • Using / in Python 3 where whole-number division // is needed.
  • Writing the prime loop as i <= sqrt(n) with floating point; i * i <= n is exact (but use a 64-bit variable for i * i when n is near the 32-bit limit).
  • Negative numbers: % and / do different things in different languages when the number is negative. Decide whether the problem allows negatives before you write the loop.

Problems

0 of 14 solved

Check yourself

What does n % 10 give for n = 4729?

Which expression removes the last digit of 4729, using whole-number division?

To test whether n is prime with n up to 10^9, trying every i from 2 to n - 1 is

Euclid's method repeatedly replaces (a, b) with (b, a % b) until b is 0. Starting from (48, 18), what is the gcd?