Tool to compute modular power. Modular Exponentiation (or power modulo) is the result of the calculus a^b mod n. It is often used in informatics and cryptography.
Modular Exponentiation - dCode
Tag(s) : Arithmetics
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
Solver limited to integer solutions < 10000
Modular exponentiation (or powmod, or modpow) is a calculation on integers composed of a power followed by a modulo. This type of calculation is widely used in modern cryptography.
A direct method is to calculate the value of the power then to extract the modulo from it (the remainder in division by n).
Example: Computing $ 9^{10} \mod 11 $ it's calculating $ 9^{10} = 3486784401 $ then $ 3486784401 \mod 11 \equiv 1 $
In practice, the numbers generated by the powers are gigantic, and mathematicians and computer scientists use simplifications, especially fast exponentiation.
The word power indicates the name of the operation, and exponent to indicate the operand.
Calculating the last $ x $ digits of $ a^b $ is equivalent to calculating $ a^b \mod n $ with $ n = 10^x $ (the number $ 1 $ followed by $ x $ zeros)
Example: $ 3^9 = 19683 $ and $ 3^9 \mod 100 = 83 $ (the last 2 digits)
There are several algorithms, but the most efficient one, called (modular) fast exponentiation, uses a property on the binary writing of $ e $.
Writing $ e=\sum_{i=0}^{m-1}a_{i}2^{i} $ over $ m $ bits with $ a_i $ the binary values (0 or 1) in writing in base 2 of $ e $ (with $ a_{m-1} = 1 $)
Then $ b^e $ can be written $$ b^e = b^{\left( \sum_{i=0}^{n-1} a_i \cdot 2^i \right)} = \prod_{i=0}^{n-1} \left( b^{2^i} \right)^{a_i} $$
And so $$ b^e \mod n \equiv \prod_{i=0}^{n-1} \left( b^{2^i} \right)^{a_i} \mod n $$
Here is the implementation of fast modular exponentiation in pseudocode:// pseudocode
function powmod(base b, exponent e, modulus m) {
r = 1
b = b % m
if (b == 0) return 0
while (e > 0) {
if (e % 2) r = (r * b) % m
e = e >> 1
b = (b ** 2) % m
}
return r
}
In theory, the fast powmod algorithm (above) is also the one with the fewest steps. It needs $ m $ steps, with $ m $ the size in bits of the number $ b $ in binary.
In practice, for small values of $ a $, $ b $ and $ n $ calculating the power then the modulo (with Euclidean division) is more instinctive.
This calculation is known as the discrete logarithm problem. Some solutions can be found by brute force but there is no trivial general solution.
Calculus uses exponent and modulus that are generally defined over the natural number domain set N. It is possible to use rational numbers but it is not handled here.
dCode retains ownership of the "Modular Exponentiation" source code. Any algorithm for the "Modular Exponentiation" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Modular Exponentiation" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Modular Exponentiation" or any other element are not public (except explicit open source licence like Creative Commons). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Modular Exponentiation" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source.
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Modular Exponentiation on dCode.fr [online website], retrieved on 2025-04-15,