Euler's totient function φ(n) counts the number of positive integers from 1 to n that are coprime with n (i.e., have gcd(k, n) = 1). Write a function `totient(n)` that returns φ(n) for a positive integer n. The solution must handle n up to 10^6 efficiently (Hint: factorize n or use a sieve-like method).
Constraints
1 ≤ n ≤ 10^6. Time limit: O(sqrt(n)) per call is acceptable for a single call, but O(n log log n) precomputation is fine if you plan multiple calls (not required). Your function should be deterministic and return an integer.