Fast Modular Exponentiation

easy
Unit 7 · BFS I0/4 to milestone

Implement mod_pow(base, exp, mod) computing base**exp % mod by square-and-multiply in O(log exp) multiplications, without calling the built-in three-argument pow. exp >= 0; mod >= 1 (anything % 1 == 0).

Then implement is_probable_prime(n) with the Fermat test for bases 2, 3, 5, 7 (return False for n < 2, True for those small primes themselves). Note that Carmichael numbers can fool it; that's expected.

#number-theory#divide-and-conquer2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Fast Modular Exponentiation · TechGradify