Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.

Sign in to watch this lesson
Algorithms - Bits and Number Theory · 4:04
Compute a^b with O(log b) multiplications by letting the bits of the exponent pick which repeated squares to multiply: 13 = 1101, so 3^13 = 3^8 * 3^4 * 3^1 = 1594323, with the 9 skipped. We trace the iterative loop, add the modulus safely (reduce a first, start from 1 % m, keep products in range), then swap numbers for 2x2 matrices and permutations to get Fibonacci numbers and repeated shuffles from the same skeleton. Measured on this Mac: 10^8 one-by-one multiplications took about 0.4 s against about 0.3 microseconds for the loop, and for moduli near 10^18 the product needs 128-bit arithmetic.