# Maths for Coding Rounds

> Cover the number theory that OAs keep reusing: GCD, primes and sieve, modular arithmetic, fast exponentiation and basic combinatorics. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/math-for-dsa
Last updated: 2026-11-28

Cover the number theory that OAs keep reusing: GCD, primes and sieve, modular arithmetic, fast exponentiation and basic combinatorics.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Bit Manipulation](https://www.internhack.in/roadmaps/tracks/dsa-placement/bit-manipulation)

Many OA problems hide a little maths. You do not need olympiad level, just a reliable toolkit.

### The toolkit

- **GCD and LCM**: Euclid's algorithm, lcm(a, b) = a / gcd(a, b) \* b (divide first to avoid overflow).
- **Primes**: trial division up to sqrt(n); **Sieve of Eratosthenes** for all primes up to 1e6 or 1e7; smallest prime factor sieve for fast factorisation.
- **Divisors**: enumerate up to sqrt(n) in pairs.
- **Modular arithmetic**: answers "modulo 1e9 + 7"; add, subtract and multiply under mod; subtract carefully to avoid negatives.
- **Fast exponentiation**: a^b mod m in O(log b).
- **Modular inverse** with Fermat's little theorem when m is prime: a^(m - 2) mod m.
- **Combinatorics**: nCr with precomputed factorials and inverse factorials, Pascal's triangle for small n.
- Digit tricks: reverse a number, check palindrome number, Armstrong numbers (common in service-company tests).

### Classic problems

Count Primes, Pow(x, n), GCD of an array, Prime Factorization using Sieve, Pascal's Triangle, Excel Sheet Column Number, Count Good Numbers, Exponentiation (CSES), Binomial Coefficients (CSES).

### Pitfalls

- Overflow before taking mod: multiply in 64-bit, then mod.
- Taking mod of a subtraction and getting a negative number.

You are ready to move on when you can write a sieve, binary exponentiation and nCr mod p from memory.

## Key concepts

- **GCD, LCM and Euclid**
- **Sieve of Eratosthenes**
- **Prime factorisation**
- **Modular arithmetic**
- **Binary exponentiation**
- **Modular inverse**
- **nCr with factorials**

## Mini project

Build a personal maths template (gcd, sieve, spf factorisation, power mod, inverse, nCr) and solve: Count Primes, Pow(x, n), Pascal's Triangle, Count Good Numbers, Exponentiation (CSES), Counting Divisors (CSES), Binomial Coefficients (CSES). Log time and pattern.

## Check yourself

Why do you compute lcm as a / gcd(a, b) \* b instead of a \* b / gcd(a, b)?

## Free resources

- [cp-algorithms: Sieve of Eratosthenes](https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html) (cp-algorithms)
- [cp-algorithms: Binary exponentiation](https://cp-algorithms.com/algebra/binary-exp.html) (cp-algorithms)
- [CSES Problem Set: Mathematics](https://cses.fi/problemset/) (CSES)

Previous: [Bit Manipulation](https://www.internhack.in/roadmaps/tracks/dsa-placement/bit-manipulation) · Next: [Segment Trees and Fenwick Trees](https://www.internhack.in/roadmaps/tracks/dsa-placement/segment-tree-fenwick)

## More in Tries, Bits, Maths and Range Queries

- [Tries](https://www.internhack.in/roadmaps/tracks/dsa-placement/tries)
- [Bit Manipulation](https://www.internhack.in/roadmaps/tracks/dsa-placement/bit-manipulation)
- [Maths for Coding Rounds](https://www.internhack.in/roadmaps/tracks/dsa-placement/math-for-dsa)
- [Segment Trees and Fenwick Trees](https://www.internhack.in/roadmaps/tracks/dsa-placement/segment-tree-fenwick)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
