Why Number Theory in Quant Interviews?
Number theory problems test logical reasoning and pattern recognition under pressure. They appear in brainteaser rounds at firms like Jane Street, Citadel, and Two Sigma — not because you'll use modular arithmetic in production code, but because the thinking patterns transfer directly to combinatorial and algorithmic problems.
Modular Arithmetic
The modulo operation gives the remainder after division: amodn=r means a=qn+r for some integer q, with 0≤r<n.
Key properties:
- (a+b)modn=((amodn)+(bmodn))modn
- (a×b)modn=((amodn)×(bmodn))modn
- (ak)modn can be computed efficiently via repeated squaring in O(logk) steps
Example — Last digit of 7100. The last digit is 7100mod10. The powers of 7 mod 10 cycle: 7,9,3,1,7,9,3,1,… with period 4. Since 100=4×25, we get 7100mod10=1.
Interview Tip: When asked about last digits, think mod 10. When asked about divisibility, think mod n. Identifying the cycle length is usually the key step.
Divisibility Rules
Quick tests without division:
- By 3 or 9: Sum the digits. If the sum is divisible by 3 (or 9), so is the number.
- By 4: Check the last two digits.
- By 7: No simple rule — use modular arithmetic directly.
- By 11: Alternating sum of digits. If d1−d2+d3−d4+⋯ is divisible by 11, so is the number.
The Euclidean Algorithm
To find gcd(a,b): repeatedly replace the larger number with the remainder of dividing the two:
gcd(a,b)=gcd(b,amodb),gcd(a,0)=a.
Example. gcd(84,36)=gcd(36,12)=gcd(12,0)=12.
The extended Euclidean algorithm finds integers x,y such that ax+by=gcd(a,b). This has applications in cryptography and modular inverses.
Fermat's Little Theorem
If p is prime and gcd(a,p)=1, then ap−1≡1(modp).
This is the workhorse for simplifying large exponents mod a prime.
Example. 2100mod13: since 13 is prime, 212≡1(mod13). Then 100=12×8+4, so 2100≡24=16≡3(mod13).
Euler's Totient and Euler's Theorem
When the modulus is composite, Fermat's little theorem doesn't apply directly. Euler's totient φ(n) counts the integers in {1,2,…,n} that are coprime to n:
- φ(p)=p−1 for prime p.
- φ(pk)=pk−pk−1=pk−1(p−1).
- φ is multiplicative: φ(mn)=φ(m)φ(n) when gcd(m,n)=1.
So for n=p1a1⋯prar, φ(n)=n∏i(1−1/pi).
Example. φ(12)=φ(4)φ(3)=2⋅2=4 (the coprime classes are {1,5,7,11}).
Euler's theorem (generalization of Fermat). If gcd(a,n)=1, then aφ(n)≡1(modn).
Example. 71000mod12: gcd(7,12)=1 and φ(12)=4, so 74≡1(mod12). Then 71000=(74)250≡1(mod12).
Fast Modular Exponentiation
To compute akmodn when k is huge, use repeated squaring -- O(logk) multiplications instead of O(k):
function modpow(a, k, n):
result = 1
a = a mod n
while k > 0:
if k is odd:
result = (result * a) mod n
a = (a * a) mod n
k = k // 2 # integer division
return result
This is the algorithm Fermat's-test-based primality checks (Miller--Rabin) use, and it's the basis of every cryptographic protocol that requires akmodn for thousand-digit n.
Example. 313mod7 via repeated squaring: write 13=11012, so 313=38⋅34⋅31. Compute 32=9≡2, 34≡4, 38≡16≡2(mod7). Then 313≡2⋅4⋅3=24≡3(mod7). ✓ (Direct check: 313=1,594,323=227,760⋅7+3.)
Worked Interview Problem
Is n=1,000,001 divisible by 101?
1,000,001=106+1. We need 106+1mod101. By Fermat's little theorem, 10100≡1(mod101). Check: 102=100≡−1(mod101), so 104≡1(mod101), and 106=(104)(102)≡1×(−1)=−1(mod101). Thus 106+1≡0(mod101). Yes, it is divisible.