Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Modular exponentiation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Memory-efficient method == Keeping the numbers smaller requires additional modular reduction operations, but the reduced size makes each operation faster, saving time (as well as memory) overall. This algorithm makes use of the identity : {{math|(''a'' β ''b'') mod ''m'' {{=}} [(''a'' mod ''m'') β (''b'' mod ''m'')] mod ''m''}} The modified algorithm is: :'''Inputs''' ''An integer {{mvar|b}} (base), integer {{mvar|e}} (exponent), and a positive integer {{mvar|m}} (modulus)'' :'''Outputs''' ''The modular exponent {{mvar|c}} where'' {{math|''c'' {{=}} ''b''<sup>''e''</sup> ''mod m''}} :# Initialise {{math|''c'' {{=}} 1}} and loop variable {{math|''eβ²'' {{=}} 0}} :# While {{math|''eβ²'' < ''e''}} do :## Increment {{mvar|eβ²}} by 1 :## Calculate {{math|''c'' {{=}} (''b'' β ''c'') mod ''m''}} :# Output {{mvar|c}} Note that at the end of every iteration through the loop, the equation {{math|''c'' β‘ ''b''{{sup|''eβ²''}} (mod ''m'')}} holds true. The algorithm ends when the loop has been executed {{mvar|e}} times. At that point {{mvar|c}} contains the result of {{math|''b''<sup>''e''</sup> mod ''m''}}. In summary, this algorithm increases {{mvar|eβ²}} by one until it is equal to {{mvar|e}}. At every step multiplying the result from the previous iteration, {{mvar|c}}, by {{mvar|b}} and performing a modulo operation on the resulting product, thereby keeping the resulting {{mvar|c}} a small integer. The example {{math|''b'' {{=}} 4}}, {{math|''e'' {{=}} 13}}, and {{math|''m'' {{=}} 497}} is presented again. The algorithm performs the iteration thirteen times: : {{math|''(eβ²'' {{=}} {{spaces|en}}1) {{spaces|em}} ''c'' {{=}} (4 β 1) mod 497 {{=}} 4 mod 497 {{=}} '''4'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}2) {{spaces|em}} ''c '' {{=}} (4 β 4) mod 497 {{=}} 16 mod 497 {{=}} '''16'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}3) {{spaces|em}} ''c'' {{=}} (4 β 16) mod 497 {{=}} 64 mod 497 {{=}} '''64'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}4) {{spaces|em}} ''c'' {{=}} (4 β 64) mod 497 {{=}} 256 mod 497 {{=}} '''256'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}5) {{spaces|em}} ''c'' {{=}} (4 β 256) mod 497 {{=}} 1024 mod 497 {{=}} '''30'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}6) {{spaces|em}} ''c'' {{=}} (4 β 30) mod 497 {{=}} 120 mod 497 {{=}} '''120'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}7) {{spaces|em}} ''c'' {{=}} (4 β 120) mod 497 {{=}} 480 mod 497 {{=}} '''480'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}8) {{spaces|em}} ''c'' {{=}} (4 β 480) mod 497 {{=}} 1920 mod 497 {{=}} '''429'''}} : {{math|''(eβ²'' {{=}} {{spaces|en}}9) {{spaces|em}} ''c'' {{=}} (4 β 429) mod 497 {{=}} 1716 mod 497 {{=}} '''225'''}} : {{math|''(eβ²'' {{=}} 10) {{spaces|em}} ''c'' {{=}} (4 β 225) mod 497 {{=}} 900 mod 497 {{=}} '''403'''}} : {{math|''(eβ²'' {{=}} 11) {{spaces|em}} ''c'' {{=}} (4 β 403) mod 497 {{=}} 1612 mod 497 {{=}} '''121'''}} : {{math|''(eβ²'' {{=}} 12) {{spaces|em}} ''c'' {{=}} (4 β 121) mod 497 {{=}} 484 mod 497 {{=}} '''484'''}} : {{math|''(eβ²'' {{=}} 13) {{spaces|em}} ''c'' {{=}} (4 β 484) mod 497 {{=}} 1936 mod 497 {{=}} '''445'''}} The final answer for {{mvar|c}} is therefore 445, as in the [[#Direct method|direct method]]. Like the first method, this requires {{math|O(''e'')}} multiplications to complete. However, since the numbers used in these calculations are much smaller than the numbers used in the first algorithm's calculations, the computation time decreases by a factor of at least {{math|O(''e'')}} in this method. In pseudocode, this method can be performed the following way: '''function''' modular_pow(base, exponent, modulus) '''is''' '''if''' modulus = 1 '''then''' '''return''' 0 c := 1 '''for''' e_prime = 0 '''to''' exponent-1 '''do''' c := (c * base) '''mod''' modulus '''return''' c
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)