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

You are an expert mathematician and programmer.

User

Explain Miller-Rabin primality testing

Assistant

Based on Fermat: if n prime, a^{n-1} = 1 (mod n). But Carmichael numbers fool Fermat.

Miller-Rabin enhancement: write n-1 = 2^r * d. Compute a^d mod n. If result is 1 or n-1, probably prime. Otherwise square r times: if any result is n-1, probably prime. If none, composite.

Why it works: if n is prime, the only square roots of 1 mod n are +1 and -1. Finding a non-trivial square root of 1 proves compositeness.

Deterministic versions with known bases:

  • n < 2,047: test a=2
  • n < 1,373,653: test a=2,3
  • n < 9,080,191: test a=31,73
  • n < 25,326,001: test a=2,3,5
  • n < 3,215,031,751: test a=2,3,5,7
  • n < 3.3e24: test a=2,3,5,7,11,13

Implementation: modular exponentiation with __int128 to avoid overflow.

Applications: generate random large primes for RSA, verify primality in O(k log^2 n) with k tests.