From 2e8234232da3e6d0ffeccaea081810862b3e6f99 Mon Sep 17 00:00:00 2001 From: Peter Norvig Date: Sun, 29 Mar 2026 19:44:23 -0700 Subject: [PATCH] Update explanation of modular exponentiation --- ipynb/TruncatablePrimes.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipynb/TruncatablePrimes.ipynb b/ipynb/TruncatablePrimes.ipynb index 7ecf305..323df2e 100644 --- a/ipynb/TruncatablePrimes.ipynb +++ b/ipynb/TruncatablePrimes.ipynb @@ -1127,7 +1127,7 @@ "\n", "# Note on Modular Exponentiation\n", "\n", - "Just one more thing: none of this would work unless we can efficiently compute *a*(*n* - 1) (mod *n*). How does the `pow` builtin function do it? When *a* and *n* are 24-digit numbers, if we naively tried to compute `a ** (n - 1)`, we'd have two problems: we'd need nearly a billion petabytes to store the result, and we'd need centuries to compute it. The way around these problems is to use [modular exponentiation](https://en.wikipedia.org/wiki/Modular_exponentiation) where we apply the modulus to each intermediate result, and cut the exponent in half each iteration, so we need only do *O*(log *n*) multiplications, not *O*(*n*). That's a big difference: 1024 is a trillion trillion, and log2(1024) is only 80. \n", + "Just one more thing: none of this would work unless we can efficiently compute *a*(*n* - 1) (mod *n*). How does the `pow` builtin function do it? When *a* and *n* are 24-digit numbers, if we naively tried to compute `a ** (n - 1)` and then apply (mod *n*), we'd have two problems: we'd need nearly a billion petabytes to store the result, and we'd need centuries to compute it. The way around these problems is to use [modular exponentiation](https://en.wikipedia.org/wiki/Modular_exponentiation) where we apply the modulus to each intermediate result, and cut the exponent in half each iteration, so we need only do *O*(log *n*) multiplications, not *O*(*n*). That's a big difference: 1024 is a trillion trillion, and log2(1024) is only 80. \n", "\n", "There are two key ides that make this work:\n", "1) *b*2*e* = (*b* × *b*)*e*\n",