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 % 10looks 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:
- Digit problems (count, sum, reverse, palindrome, Armstrong): peel digits with
% 10and/ 10. - Divisibility problems (perfect number, divisors):
n % d == 0says thatddividesn. - Prime and divisor search: factors come in pairs, so you only need to look up to the square root of
n. - 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.
| n | last digit | total after adding it |
|---|---|---|
| 4729 | 9 | 9 |
| 472 | 2 | 11 |
| 47 | 7 | 18 |
| 4 | 4 | 22 |
| 0 | stop | 22 |
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
0as having no digits. A digit loop that runswhile n > 0does 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 * bin 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 <= nis exact (but use a 64-bit variable fori * iwhennis 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
- Count all Digits of a Numbereasyguide ready
- Count number of odd digits in a numbereasyguide ready
- Palindrome Numbereasyguide ready
- Return the Largest Digit in a Numbereasyguide ready
- Factorial of a given numbereasyguide ready
- Armstrong Numbereasyguide ready
- Perfect Numbereasyguide ready
- Check for Prime Numbereasyguide ready
- GCD of Two Numberseasyguide ready
- LCM of two numberseasyguide ready
- Divisors of a Numbereasyguide ready
- Find factorial of a large numbereasyguide ready
- Reverse Integermediumguide ready
- Pow(x, n)mediumguide ready