Step 01 Beginner Problems — Concept Basics
Basic Maths
Basic Maths is the first real problem family in the sheet, and it rests on a handful of recipes you will reuse for years: extracting digits with % and //, enumerating divisors up to √n, testing primality, and Euclid's GCD. Every recipe here comes with its complexity and its classic trap. Master this lesson and the entire Basic Maths subtopic (eleven problems) becomes variations on themes you already know.
The digit-extraction framework
One loop powers half the family. For any number n: the last digit is n % 10, and removing it is n // 10. Loop while n > 0, updating an accumulator per digit:
Count digits — increment a counter (or use len(str(n)) as a shortcut, but know the loop). Sum of digits — add the digit. Reverse — acc = acc * 10 + digit. Largest digit — keep a running maximum. Count odd digits — add 1 when digit % 2 == 1.
Two traps: the loop destroys n, so copy it first if the original is needed afterwards (palindrome checks!); and n = 0 never enters the loop — handle it as a special case (0 has one digit, and it is a palindrome). A neat alternative for counting digits of positive n is floor(log10(n)) + 1, but the loop generalises to every other recipe, so learn the loop.
def reverse_number(n):
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
return rev
print(reverse_number(1234)) # 4321Armstrong and perfect numbers
An Armstrong (narcissistic) number equals the sum of its own digits each raised to the power of the digit count: 153 = 1^3 + 5^3 + 3^3. Algorithm: count digits (k), then run digit extraction accumulating digit ** k, and compare with a saved copy of the original.
A perfect number equals the sum of its proper divisors (28 = 1 + 2 + 4 + 7 + 14). That recipe belongs to the next section — enumerate divisors in O(√n), sum them excluding n itself, compare. Notice both are 'accumulate a per-piece contribution, compare with the original' — the same shape as the digit recipes, one level up.
Divisors and primes in O(√n)
Divisors come in pairs: if d divides n, so does n / d, and one of each pair is ≤ √n. So loop d from 1 to √n; whenever n % d == 0, record both d and n // d (once, when they are equal — the perfect-square case). That is O(√n) instead of O(n): for n = 10^12, a million iterations instead of a trillion.
Prime testing uses the same bound: a composite n must have a factor ≤ √n, so trial-divide only up to √n (skip evens after checking 2, for a free 2× speedup). Counting all primes up to N is different: testing each number costs O(N√N), while the Sieve of Eratosthenes — mark multiples of each prime starting from its square — costs about O(N log log N) and is the intended solution.
def is_prime(n):
if n < 2:
return False
d = 2
while d * d <= n:
if n % d == 0:
return False
d += 1 if d == 2 else 2 # check 2, then odds only
return True
print(is_prime(29)) # True
print(is_prime(51)) # False (3 x 17)GCD, LCM and factorial
Euclid's algorithm: gcd(a, b) = gcd(b, a mod b), repeating until the remainder is 0 — the last non-zero value wins. It runs in O(log(min(a,b))) steps because the numbers shrink fast; the naive 'try every d from 1 to min' is O(n) and dies on large inputs. LCM then comes free from the identity a × b = gcd(a,b) × lcm(a,b), computed as a // gcd * b — divide before multiplying to dodge overflow in fixed-width languages.
Factorial is the simplest accumulation: result = 1 × 2 × ... × n, O(n) time, and it grows so fast (21! overflows a 64-bit integer) that problems usually ask for it modulo something, or for properties like trailing zeros instead of the value itself.
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a // gcd(a, b) * b
print(gcd(48, 18), lcm(4, 6)) # 6 12The trap list for this whole family
Forgetting to save the original number before a destructive loop. Off-by-one at √n: use d * d <= n, not <, or perfect squares slip through (49 would test as prime-ish in divisor sums). Treating 1 as prime (it is not) and 0/negatives as valid inputs without checking the problem statement. Returning divisors unsorted — the pair enumeration produces them out of order, so sort at the end if the problem asks. And reaching for O(n) loops where the constraint (n up to 10^12) is quietly demanding O(√n).
Key points
- One loop, many recipes: digit = n % 10, update accumulator, n //= 10 — count, sum, reverse, largest all share it.
- Save the original before destroying n; handle n = 0 explicitly — the loop never runs for it.
- Divisors come in pairs around √n: enumerate to √n and record d and n//d (once when equal).
- Prime test: trial division to √n only; counting primes to N wants the Sieve, not N separate tests.
- Euclid's GCD is O(log) steps; LCM = a // gcd * b — divide before multiplying to avoid overflow.
- Factorial explodes fast (21! > 64-bit) — expect modulo or property questions, not raw values.