ExamplesBy LevelBy TopicLearning Paths
830 Fundamental

Euler's Totient Function

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Euler's Totient Function" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. Euler's totient function phi(n) counts the integers from 1 to n that are coprime to n. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Euler's totient function phi(n) counts the integers from 1 to n that are coprime to n. It is fundamental to RSA key generation: the private key d satisfies e * d ≡ 1 (mod phi(n)) where n = p*q. By Euler's theorem, a^phi(n) ≡ 1 (mod n) for any a coprime to n, generalizing Fermat's little theorem. phi(n) also counts primitive roots, answers "how many fractions in lowest terms have denominator n," and appears in the analysis of the Stern-Brocot tree. For prime p, phi(p) = p-1; for prime power p^k, phi(p^k) = p^k - p^(k-1); these combine multiplicatively for general n.

🎯 Learning Outcomes

  • • Compute phi(n) from prime factorization: phi(n) = n * product of (1 - 1/p) for each prime factor p
  • • Implement the sieve-based batch computation of phi(1..n) in O(n log log n)
  • • Apply Euler's theorem: a^phi(n) ≡ 1 (mod n) for gcd(a,n) = 1
  • • Use phi for RSA private key derivation: d = e^(-1) mod phi(n)
  • • Understand multiplicativity: phi(ab) = phi(a)phi(b) when gcd(a,b) = 1
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Per-n computationTrial division O(sqrt n)Same approach
    Batch 1..nSieve with Vec<u64>Array.init + sieve loop
    Factor applicationresult -= result / dr := !r - !r / d
    MultiplicativityExplicit in algorithmSame
    Standard libraryNo built-inNo built-in
    RSA integrationUsed in key generation testsSame theoretical role

    OCaml Approach

    OCaml's euler_totient n mirrors the Rust approach using mutable let r = ref n and let n = ref n. The sieve version initializes phi.(i) = i and iterates: for each i, if phi.(i) = i (i is prime), for each multiple j: phi.(j) <- phi.(j) / i * (i-1). OCaml's Array.init n (fun i -> i) creates the initial array. The product formula phi.(n) = n * fold primes (fun acc p -> acc * (p-1) / p) is idiomatic in functional style.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Euler Totient

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Compute the sum of phi(k) for k=1..n and verify it equals n*(n+1)/2 for prime n.
  • Implement the sieve for phi(1..n) in O(n log log n) and use it to answer sum(phi(k), k=1..n) queries.
  • Verify Euler's theorem: compute a^phi(n) mod n = 1 for several coprime (a,n) pairs.
  • Use phi to count the number of primitive roots modulo a prime p (answer: phi(p-1)).
  • Implement RSA key generation: choose primes p,q; compute phi=phi(pq)=(p-1)(q-1); find e, d=e^(-1) mod phi.
  • Open Source Repos