TRT
Sign in

Core · Module 12 · Lesson 1 of 4

Euclidean GCD, Modular Arithmetic, and Binary Exponentiation

Compute greatest common divisors in logarithmic steps, prevent 64-bit overflow with modular arithmetic, and raise numbers to huge powers in O(log b) multiplications.

Pro — members onlyAbout 18 minutes

By the end you can

  • Prove and trace Euclid's GCD algorithm gcd(a, b) = gcd(b, a mod b) in O(log(min(a, b))) steps
  • Apply modular addition, subtraction, and multiplication rules without signed 64-bit overflow
  • Compute a^b mod m in O(log b) time using binary exponentiation and modular inverse via Fermat's Little Theorem

Builds on Bits, bytes and number representation, Counting steps.

01 · A question first

Computing 3^(10^9) mod 1,000,000,007 with a simple loop takes a billion multiplications and times out; binary exponentiation finishes in just 30 multiplications. How does reading the binary bits of the exponent skip 99.999997% of the work?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Intuitive Explanation & Real-World Analogy
  • Core Concepts & Visual Walkthrough
  • Pseudocode
  • Multi-Language Implementations (C++, Java, Python)
  • Time & Auxiliary Space Complexity Derivation
  • Edge Cases & Common Beginner Mistakes
  • Alternative Approaches (Brute Force vs. Optimal)
  • Pattern Recognition Guide & Three-Stage Practice Plan

03 · Practice

Something wrong with this lesson? Report a problem