Loading
Loading
A toy encryption scheme raises a message number to a large exponent and keeps only the remainder modulo a key. The exponent can be in the billions, so multiplying one step at a time is out of the question.
Given base, exponent and modulus, return base^exponent mod modulus. Treat 0⁰ as 1, so the answer for exponent 0 is 1 mod modulus.
base = 3, exponent = 13, modulus = 1013 = 1101₂3¹³ = 3⁸ · 3⁴ · 3¹bit 0 = 1: result = 3square base: 3² = 9bit 1 = 0: result = 3square base: 9² mod 10 = 1bit 2 = 1: result = 3 · 1 = 3square base: 1bit 3 = 1: result = 3 · 1 = 3exponent exhausted3Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
3¹³ = 1594323, which leaves remainder 3.
Example 2
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(log exponent) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
2¹⁰ = 1024, and 1024 mod 1000 = 24.