tinycodinglm / math /math-0150.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.31 kB
opencode trace
System You are an expert mathematician and programmer.

You are an expert mathematician and programmer.

User

Explain big integer arithmetic algorithms

Assistant

Representation: vector of digits in base 10^9 (or base 2^32/2^64). Least significant first.

Addition: O(n). Carry propagation.
Subtraction: O(n). Borrow propagation.
Multiplication methods:

  • Naive O(n^2): schoolbook method
  • Karatsuba O(n^1.585): split numbers, 3 multiplications instead of 4
  • Toom-Cook O(n^1.465): split into 3 parts, 5 multiplications
  • FFT-based: O(n log n). Convert to pointwise, multiply, interpolate back. Used for very large numbers.

Division: Knuth Algorithm D. Or use Newton's method with reciprocal approximation.

Modular: Montgomery form for batch operations.

GCD: 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.

Applications: cryptography (RSA with 2048+ bit keys), competitive programming (problems requiring arbitrary precision), hashing.