{"type": "session", "harness": "opencode", "id": "math-0150", "name": "Big Integer Arithmetic"} {"type": "message", "message": {"role": "system", "content": "You are an expert mathematician and programmer."}} {"type": "message", "message": {"role": "user", "content": "Explain big integer arithmetic algorithms"}} {"type": "message", "message": {"role": "assistant", "content": "Representation: vector of digits in base 10^9 (or base 2^32/2^64). Least significant first.\n\nAddition: O(n). Carry propagation.\nSubtraction: O(n). Borrow propagation.\nMultiplication methods:\n- Naive O(n^2): schoolbook method\n- Karatsuba O(n^1.585): split numbers, 3 multiplications instead of 4\n- Toom-Cook O(n^1.465): split into 3 parts, 5 multiplications\n- FFT-based: O(n log n). Convert to pointwise, multiply, interpolate back. Used for very large numbers.\n\nDivision: Knuth Algorithm D. Or use Newton's method with reciprocal approximation.\n\nModular: Montgomery form for batch operations.\n\nGCD: binary GCD (Stein's algorithm): uses shifts and subtraction instead of modulo. gcd(a,b) = gcd(a, b-a) when both even, gcd(a/2, b) when a even, gcd((a-b)/2, b) when both odd.\n\nApplications: cryptography (RSA with 2048+ bit keys), competitive programming (problems requiring arbitrary precision), hashing."}}