Number Tricks: Digits, Carries, Bases and Modular Arithmetic

Algorithms
50 / 68

Most number puzzles reduce to a handful of moves: peel digits with divmod, carry like schoolbook addition, convert bases by repeated division, and keep numbers small with the remainder operator.

The Analogy (Read This First)

An odometer. Each wheel shows one digit, and when a wheel passes 9 it rolls to 0 and nudges the next wheel by one. Every digit and carry problem is that odometer.

Deep Dive Analysis

Peel digits with divmod. divmod(n, 10) gives the last digit and the rest in one step. Repeating it until the number is 0 visits every digit from the right: the digit sum of 9875 is 29. Reverse the order of work (read digits, then rebuild) to reverse a number.

def digit_sum(n):
    total = 0
    while n:
        n, d = divmod(n, 10)
        total += d
    return total

digit_sum(9875)   # 29

Carry like on paper. To add two numbers given as digit strings (too long for any integer type), start at the right ends, add the two digits plus the carry, keep sum % 10 and carry sum // 10. Keep going while either number has digits left or a carry remains: 999 + 1 ends with a carry that makes a fourth digit (1000).

def add_digits(a, b):
    out, carry = [], 0
    i, j = len(a) - 1, len(b) - 1
    while i >= 0 or j >= 0 or carry:
        s = carry
        if i >= 0:
            s += int(a[i]); i -= 1
        if j >= 0:
            s += int(b[j]); j -= 1
        out.append(str(s % 10))
        carry = s // 10
    return ''.join(reversed(out))

add_digits('999', '1')    # '1000'
add_digits('456', '77')   # '533'

Other bases are the same loop. Repeatedly divide by the base and collect remainders; the digits come out last-first, so reverse them. 255 is 11111111 in base 2 and FF in base 16; 100 is 202 in base 7 (2 x 49 + 0 x 7 + 2). Going the other way, multiply the running value by the base and add each digit.

Division and remainder with negatives. Python floors: -7 // 2 is -4 and -7 % 2 is 1, while int(-7 / 2) truncates toward zero and gives -3. Many languages truncate instead, so a solution that is right in one language can be off by one in another. Prefer integer arithmetic over floats whenever the answer must be exact.

Keep numbers small with a modulus. (a + b) % m and (a * b) % m can be taken at every step, which is how you compute huge powers: pow_mod squares the base and halves the exponent, so 2^10 mod 1000 takes four rounds instead of a million-digit number. Python also has pow(base, exp, mod) built in.

def pow_mod(base, exp, mod):
    result = 1
    base %= mod
    while exp:
        if exp & 1:
            result = result * base % mod
        base = base * base % mod
        exp >>= 1
    return result

pow_mod(3, 200, 13)   # 9

Euclid's gcd and the sieve. gcd(a, b): replace (a, b) with (b, a % b) until b is 0 (gcd(48, 18) is 6). To list all primes up to n, cross out the multiples of each prime starting at p * p: the primes up to 30 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. Python caps integer-to-text conversion at 4,300 digits by default, so for huge numbers keep digit lists and do the carries yourself.

Key Bullets
  • divmod(n, 10) peels the last digit; carry is sum // 10 and the digit is sum % 10.
  • A base conversion is repeated division, with the remainders read backwards.
  • Python floors on negatives (-7 // 2 is -4); other languages may truncate.
  • Take % m at every step to keep products small; pow(b, e, m) is built in.
  • Prefer integers over floats when an answer must be exact.
Trade-offs
Digit-by-digit arithmetic: works at any length | More code than a single + on integersModular power by squaring: O(log exp) | Only gives the remainder, not the full numberSieve: lists all primes up to n quickly | Needs O(n) memory
Real-World Examples
Adding two 1,000-digit numbers given as textConverting an ID to base 62 for a short linkComputing 3^200 mod 13 without a huge number

Curated Curation & Deep Insights

ScaleDojo Certified
Video Tutorial Pending

Our system architects are vetting high-quality, authorized video guides for Number Tricks: Digits, Carries, Bases and Modular Arithmetic with zero third-party platform links.

Verification in Progress
Reading Material Pending

We are preparing premium, zero-competitor deep-dives for Number Tricks: Digits, Carries, Bases and Modular Arithmetic. Authorized reading references will appear automatically.

Verification in Progress