Math Fundamentals18 sections · 814 units
Open in Course

Power Modulo - Walkthrough

Tracing the algorithm

Let me trace powerMod(3,13,1000)powerMod(3, 13, 1000):

Binary of 13: 110121101_2. Process bits right to left.

1.1. n=13n = 13 (odd), result=1×3=3result = 1 \times 3 = 3, x=32mod1000=9x = 3^2 \bmod 1000 = 9, n=6n = 6

2.2. n=6n = 6 (even), result=3result = 3, x=92mod1000=81x = 9^2 \bmod 1000 = 81, n=3n = 3

3.3. n=3n = 3 (odd), result=3×81=243result = 3 \times 81 = 243, x=812mod1000=561x = 81^2 \bmod 1000 = 561, n=1n = 1

4.4. n=1n = 1 (odd), result=243×561=136323mod1000=323result = 243 \times 561 = 136323 \bmod 1000 = 323

Final answer: 323323. You computed 313mod10003^{13} \bmod 1000 in just 44 steps instead of 1313 multiplications.