ExamplesBy LevelBy TopicLearning Paths
825 Intermediate

Prime Factorization

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Prime Factorization" functional Rust example. Difficulty level: Intermediate. Key concepts covered: Functional Programming. Decomposing an integer into its prime factors is fundamental to number theory: computing GCD, LCM, Euler's totient, cryptographic key generation, and combinatorial coefficient calculations all rely on factorization. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Decomposing an integer into its prime factors is fundamental to number theory: computing GCD, LCM, Euler's totient, cryptographic key generation, and combinatorial coefficient calculations all rely on factorization. For small numbers, trial division by primes up to sqrt(n) suffices. For large numbers (64-bit), Pollard's rho algorithm factorizes in O(n^(1/4)) expected time. Real-world cryptography relies on the hardness of factorizing large composites (RSA). Competitive programming uses factorization for: divisor enumeration, Mobius function, Euler's phi, and modular arithmetic.

🎯 Learning Outcomes

  • • Implement trial division factorization in O(sqrt(n)) by testing primes up to sqrt(n)
  • • Represent the factorization as a map from prime to exponent: HashMap<u64, u32>
  • • Apply factorization to compute number of divisors, sum of divisors, Euler's phi
  • • Understand when to use sieve-based factorization (many numbers up to N) vs. per-number trial division
  • • Recognize the smallest prime factor sieve for batch factorization in O(log n) per number
  • Code Example

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

    Key Differences

    AspectRustOCaml
    Factor mapHashMap<u64, u32>Hashtbl or (int * int) list
    MutationTakes mut n: u64 by valuelet n = ref n
    Entry update.entry().or_insert(0) += 1Hashtbl.replace or match
    Large numbersu64 (max ~1.8 * 10^19)Zarith for arbitrary precision
    Sieve integrationCan import SPF sieve functionArray.get spf n
    Remaining primeif n > 1 check after loopSame pattern

    OCaml Approach

    OCaml uses a Hashtbl or returns a sorted (int * int) list of (prime, exponent) pairs. The recursive functional version divides out factors recursively, building the list with pattern matching. Trial division uses a while loop with mutable n ref. OCaml's Map.Make(Int) provides an immutable sorted factor map. The let () = while !n mod d = 0 do ... done pattern mirrors the Rust approach. For large numbers, OCaml's Zarith library handles arbitrary-precision factorization.

    Full Source

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

    Deep Comparison

    OCaml vs Rust: Prime Factorization

    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

  • Use factorization to compute the number of divisors of n: product of (exponent + 1) over all prime factors.
  • Implement Euler's totient phi(n) from the prime factorization: n * product of (1 - 1/p) for each prime p.
  • Compute LCM of a list of numbers using their prime factorizations (max exponent per prime).
  • Implement the smallest prime factor sieve and compare batch factorization speed vs. per-number trial division for 10^6 numbers.
  • Implement Pollard's rho algorithm for factorizing 64-bit numbers and measure speedup on large semiprimes.
  • Open Source Repos