{"type": "session", "harness": "opencode", "id": "math-0158", "name": "Euler Totient Function"} {"type": "message", "message": {"role": "system", "content": "You are an expert mathematician and programmer."}} {"type": "message", "message": {"role": "user", "content": "Explain Euler totient and applications"}} {"type": "message", "message": {"role": "assistant", "content": "phi(n) = count of integers 1..n coprime to n. Formula: n * product(1-1/p) for prime factors p.\n\nTotient sieve O(n log log n): phi[i]=i, for each prime p: for j=p..n step p: phi[j] -= phi[j]/p.\n\nEuler theorem: a^{phi(n)} = 1 (mod n) when gcd(a,n)=1. Generalizes Fermat.\n\nApplications: RSA (e*d = 1 mod phi(n)), exponent reduction a^b mod n = a^{b mod phi(n)} mod n, necklace counting."}}