⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 20m 26s17 days 22 hours 20 minutes 26 seconds.
⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 20m 26s17 days 22 hours 20 minutes 26 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.