Okay, so you want to calculate a^b mod m. First we'll take a naive approach and then see how we can refine it.

First, reduce a mod m. That means, find a number a1 so that 0 <= a1 < m and a = a1 mod m. Then repeatedly in a loop multiply by a1 and reduce again mod m. Thus, in pseudocode:

a1 = a reduced mod m
p = 1
for(int i = 1; i <= b; i++) {
    p *= a1
    p = p reduced mod m
}

By doing this, we avoid numbers larger than m^2. This is the key. The reason we avoid numbers larger than m^2 is because at every step 0 <= p < m and 0 <= a1 < m.

As an example, let's compute 5^55 mod 221. First, 5 is already reduced mod 221.

  1. 1 * 5 = 5 mod 221
  2. 5 * 5 = 25 mod 221
  3. 25 * 5 = 125 mod 221
  4. 125 * 5 = 183 mod 221
  5. 183 * 5 = 31 mod 221
  6. 31 * 5 = 155 mod 221
  7. 155 * 5 = 112 mod 221
  8. 112 * 5 = 118 mod 221
  9. 118 * 5 = 148 mod 221
  10. 148 * 5 = 77 mod 221
  11. 77 * 5 = 164 mod 221
  12. 164 * 5 = 157 mod 221
  13. 157 * 5 = 122 mod 221
  14. 122 * 5 = 168 mod 221
  15. 168 * 5 = 177 mod 221
  16. 177 * 5 = 1 mod 221
  17. 1 * 5 = 5 mod 221
  18. 5 * 5 = 25 mod 221
  19. 25 * 5 = 125 mod 221
  20. 125 * 5 = 183 mod 221
  21. 183 * 5 = 31 mod 221
  22. 31 * 5 = 155 mod 221
  23. 155 * 5 = 112 mod 221
  24. 112 * 5 = 118 mod 221
  25. 118 * 5 = 148 mod 221
  26. 148 * 5 = 77 mod 221
  27. 77 * 5 = 164 mod 221
  28. 164 * 5 = 157 mod 221
  29. 157 * 5 = 122 mod 221
  30. 122 * 5 = 168 mod 221
  31. 168 * 5 = 177 mod 221
  32. 177 * 5 = 1 mod 221
  33. 1 * 5 = 5 mod 221
  34. 5 * 5 = 25 mod 221
  35. 25 * 5 = 125 mod 221
  36. 125 * 5 = 183 mod 221
  37. 183 * 5 = 31 mod 221
  38. 31 * 5 = 155 mod 221
  39. 155 * 5 = 112 mod 221
  40. 112 * 5 = 118 mod 221
  41. 118 * 5 = 148 mod 221
  42. 148 * 5 = 77 mod 221
  43. 77 * 5 = 164 mod 221
  44. 164 * 5 = 157 mod 221
  45. 157 * 5 = 122 mod 221
  46. 122 * 5 = 168 mod 221
  47. 168 * 5 = 177 mod 221
  48. 177 * 5 = 1 mod 221
  49. 1 * 5 = 5 mod 221
  50. 5 * 5 = 25 mod 221
  51. 25 * 5 = 125 mod 221
  52. 125 * 5 = 183 mod 221
  53. 183 * 5 = 31 mod 221
  54. 31 * 5 = 155 mod 221
  55. 155 * 5 = 112 mod 221

Therefore, 5^55 = 112 mod 221.

Now, we can improve this by using exponentiation by squaring; this is the famous trick wherein we reduce exponentiation to requiring only log b multiplications instead of b. Note that with the algorithm that I described above, the exponentiation by squaring improvement, you end up with the right-to-left binary method.

a1 = a reduced mod m
p = 1
while (b > 0) {
     if (b is odd) {
         p *= a1
         p = p reduced mod m
     }
     b /= 2
     a1 = (a1 * a1) reduced mod m
}

Thus, since 55 = 110111 in binary

  1. 1 * (5^1 mod 221) = 5 mod 221
  2. 5 * (5^2 mod 221) = 125 mod 221
  3. 125 * (5^4 mod 221) = 112 mod 221
  4. 112 * (5^16 mod 221) = 112 mod 221
  5. 112 * (5^32 mod 221) = 112 mod 221

Therefore the answer is 5^55 = 112 mod 221. The reason this works is because

55 = 1 + 2 + 4 + 16 + 32

so that

5^55 = 5^(1 + 2 + 4 + 16 + 32) mod 221
     = 5^1 * 5^2 * 5^4 * 5^16 * 5^32 mod 221
     = 5 * 25 * 183 * 1 * 1 mod 221
     = 22875 mod 221
     = 112 mod 221

In the step where we calculate 5^1 mod 221, 5^2 mod 221, etc. we note that 5^(2^k) = 5^(2^(k-1)) * 5^(2^(k-1)) because 2^k = 2^(k-1) + 2^(k-1) so that we can first compute 5^1 and reduce mod 221, then square this and reduce mod 221 to obtain 5^2 mod 221, etc.

The above algorithm formalizes this idea.

Answer from jason on Stack Overflow
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1370714 โ€บ how-to-quickly-calculate-the-modulus-without-calculator
How to quickly calculate the modulus without calculator?? | Sololearn: Learn to code for FREE!
You can search it in the internet about modulus operation in mathematics, but maybe I'll give some lessons here Note: I am telling you how to calculate fastly so I assume you already know the basics of how to find a remainder In mathematics a โ‰ก b (mod c) means there exist an integer k so that a = bk+c so b doesn't necessarily have to be a remainder of a divided by c, but if b < c then b is called the remainder example 9 โ‰ก 1 (mod 2) => 9 = (4)*2+1 but could also be 9 โ‰ก 3 (mod 2) => 9 = (3)*2+3 9 โ‰ก 5 (mod 2) => 9 = (2)*2+5 and so on...
People also ask

How do you calculate the modulo for a negative number?
Calculating the modulo for a negative number follows a specific rule to ensure the remainder is non-negative. The formula used is a mod n = a - n * floor(a/n), where 'floor' is the function that rounds down to the nearest integer. For example, to find -17 mod 5:First, divide -17 by 5, which is -3.4.The floor of -3.4 is -4.Now, apply the formula: -17 - 5 * (-4) = -17 + 20 = 3.So, -17 mod 5 = 3.
๐ŸŒ
vedantu.com
vedantu.com โ€บ maths โ€บ modulo calculator for quick maths solutions
Modulo Calculator: Solve Remainders Fast with Examples
What is the modulo operation and how is it calculated?
The modulo operation finds the remainder after the division of one number by another. To calculate it, you divide the first number (the dividend) by the second number (the divisor). The whole number that is left over is the result of the modulo operation. For example, to calculate 17 mod 5, you divide 17 by 5. The result is 3 with a remainder of 2. Therefore, 17 mod 5 = 2.
๐ŸŒ
vedantu.com
vedantu.com โ€บ maths โ€บ modulo calculator for quick maths solutions
Modulo Calculator: Solve Remainders Fast with Examples
What is the difference between the mathematical 'modulo' and the '%' operator in programming?
The primary difference lies in how they handle negative numbers. While both operations find the remainder, their results can differ for negative inputs depending on the programming language:Mathematical Modulo: As defined by number theory, the result is always non-negative (e.g., -17 mod 5 = 3).Programming '%' Operator: In many languages like C++, Java, and JavaScript, the '%' is a 'remainder' operator. The sign of its result matches the sign of the dividend. For example, -17 % 5 would yield -2 in these languages. In Python, however, the '%' operator behaves like the mathematical modulo, and t
๐ŸŒ
vedantu.com
vedantu.com โ€บ maths โ€บ modulo calculator for quick maths solutions
Modulo Calculator: Solve Remainders Fast with Examples
Top answer
1 of 10
106

Okay, so you want to calculate a^b mod m. First we'll take a naive approach and then see how we can refine it.

First, reduce a mod m. That means, find a number a1 so that 0 <= a1 < m and a = a1 mod m. Then repeatedly in a loop multiply by a1 and reduce again mod m. Thus, in pseudocode:

a1 = a reduced mod m
p = 1
for(int i = 1; i <= b; i++) {
    p *= a1
    p = p reduced mod m
}

By doing this, we avoid numbers larger than m^2. This is the key. The reason we avoid numbers larger than m^2 is because at every step 0 <= p < m and 0 <= a1 < m.

As an example, let's compute 5^55 mod 221. First, 5 is already reduced mod 221.

  1. 1 * 5 = 5 mod 221
  2. 5 * 5 = 25 mod 221
  3. 25 * 5 = 125 mod 221
  4. 125 * 5 = 183 mod 221
  5. 183 * 5 = 31 mod 221
  6. 31 * 5 = 155 mod 221
  7. 155 * 5 = 112 mod 221
  8. 112 * 5 = 118 mod 221
  9. 118 * 5 = 148 mod 221
  10. 148 * 5 = 77 mod 221
  11. 77 * 5 = 164 mod 221
  12. 164 * 5 = 157 mod 221
  13. 157 * 5 = 122 mod 221
  14. 122 * 5 = 168 mod 221
  15. 168 * 5 = 177 mod 221
  16. 177 * 5 = 1 mod 221
  17. 1 * 5 = 5 mod 221
  18. 5 * 5 = 25 mod 221
  19. 25 * 5 = 125 mod 221
  20. 125 * 5 = 183 mod 221
  21. 183 * 5 = 31 mod 221
  22. 31 * 5 = 155 mod 221
  23. 155 * 5 = 112 mod 221
  24. 112 * 5 = 118 mod 221
  25. 118 * 5 = 148 mod 221
  26. 148 * 5 = 77 mod 221
  27. 77 * 5 = 164 mod 221
  28. 164 * 5 = 157 mod 221
  29. 157 * 5 = 122 mod 221
  30. 122 * 5 = 168 mod 221
  31. 168 * 5 = 177 mod 221
  32. 177 * 5 = 1 mod 221
  33. 1 * 5 = 5 mod 221
  34. 5 * 5 = 25 mod 221
  35. 25 * 5 = 125 mod 221
  36. 125 * 5 = 183 mod 221
  37. 183 * 5 = 31 mod 221
  38. 31 * 5 = 155 mod 221
  39. 155 * 5 = 112 mod 221
  40. 112 * 5 = 118 mod 221
  41. 118 * 5 = 148 mod 221
  42. 148 * 5 = 77 mod 221
  43. 77 * 5 = 164 mod 221
  44. 164 * 5 = 157 mod 221
  45. 157 * 5 = 122 mod 221
  46. 122 * 5 = 168 mod 221
  47. 168 * 5 = 177 mod 221
  48. 177 * 5 = 1 mod 221
  49. 1 * 5 = 5 mod 221
  50. 5 * 5 = 25 mod 221
  51. 25 * 5 = 125 mod 221
  52. 125 * 5 = 183 mod 221
  53. 183 * 5 = 31 mod 221
  54. 31 * 5 = 155 mod 221
  55. 155 * 5 = 112 mod 221

Therefore, 5^55 = 112 mod 221.

Now, we can improve this by using exponentiation by squaring; this is the famous trick wherein we reduce exponentiation to requiring only log b multiplications instead of b. Note that with the algorithm that I described above, the exponentiation by squaring improvement, you end up with the right-to-left binary method.

a1 = a reduced mod m
p = 1
while (b > 0) {
     if (b is odd) {
         p *= a1
         p = p reduced mod m
     }
     b /= 2
     a1 = (a1 * a1) reduced mod m
}

Thus, since 55 = 110111 in binary

  1. 1 * (5^1 mod 221) = 5 mod 221
  2. 5 * (5^2 mod 221) = 125 mod 221
  3. 125 * (5^4 mod 221) = 112 mod 221
  4. 112 * (5^16 mod 221) = 112 mod 221
  5. 112 * (5^32 mod 221) = 112 mod 221

Therefore the answer is 5^55 = 112 mod 221. The reason this works is because

55 = 1 + 2 + 4 + 16 + 32

so that

5^55 = 5^(1 + 2 + 4 + 16 + 32) mod 221
     = 5^1 * 5^2 * 5^4 * 5^16 * 5^32 mod 221
     = 5 * 25 * 183 * 1 * 1 mod 221
     = 22875 mod 221
     = 112 mod 221

In the step where we calculate 5^1 mod 221, 5^2 mod 221, etc. we note that 5^(2^k) = 5^(2^(k-1)) * 5^(2^(k-1)) because 2^k = 2^(k-1) + 2^(k-1) so that we can first compute 5^1 and reduce mod 221, then square this and reduce mod 221 to obtain 5^2 mod 221, etc.

The above algorithm formalizes this idea.

2 of 10
31

To add to Jason's answer:

You can speed the process up (which might be helpful for very large exponents) using the binary expansion of the exponent. First calculate 5, 5^2, 5^4, 5^8 mod 221 - you do this by repeated squaring:

 5^1 = 5(mod 221)
 5^2 = 5^2 (mod 221) = 25(mod 221)
 5^4 = (5^2)^2 = 25^2(mod 221) = 625 (mod 221) = 183(mod221)
 5^8 = (5^4)^2 = 183^2(mod 221) = 33489 (mod 221) = 118(mod 221)
5^16 = (5^8)^2 = 118^2(mod 221) = 13924 (mod 221) = 1(mod 221)
5^32 = (5^16)^2 = 1^2(mod 221) = 1(mod 221)

Now we can write

55 = 1 + 2 + 4 + 16 + 32

so 5^55 = 5^1 * 5^2 * 5^4 * 5^16 * 5^32 
        = 5   * 25  * 625 * 1    * 1 (mod 221)
        = 125 * 625 (mod 221)
        = 125 * 183 (mod 183) - because 625 = 183 (mod 221)
        = 22875 ( mod 221)
        = 112 (mod 221)

You can see how for very large exponents this will be much faster (I believe it's log as opposed to linear in b, but not certain.)

๐ŸŒ
YouTube
youtube.com โ€บ watch
Calculating modulus - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published ย  November 7, 2012
๐ŸŒ
VEDANTU
vedantu.com โ€บ maths โ€บ modulo calculator for quick maths solutions
Modulo Calculator: Solve Remainders Fast with Examples
December 3, 2021 - It is quite a simple topic to learn ... even without using any modular arithmetic calculator. For example, when calculating hours, we tend to count up to twelve and then begin over at one. As a result, it is 1 p.m. 4 hours past 9 pm. Congruent modulo 12 refers to integers that vary by a factor of the modulus ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/calculators โ€บ does anyone know how to do modulo operations in this calculator?
r/calculators on Reddit: Does anyone know how to do Modulo operations in this Calculator?
February 14, 2023 - Press the "S<=>D" button to show it in decimal form, 10.52941176. Multiply 17 by the whole number 10 and subtract it from 179. 17 * 10 = 170, 179 - 170 = 9. 179 mod 17 = 9. ... I see, so there isn't a dedicated modulus function in this one.
๐ŸŒ
Mathematics LibreTexts
math.libretexts.org โ€บ campus bookshelves โ€บ lumen learning โ€บ introduction to college mathematics (lumen) โ€บ 7: module 5: modular arithmetic
7.1: Calculator Shortcut for Modular Arithmetic - Mathematics LibreTexts
January 14, 2021 - Recall that when we divide 17 by 5, we could represent the result as 3 remainder 2, as the mixed number , or as the decimal 3.4. Notice that the modulus, 2, is the same as the numerator of the fractional part of the mixed number, and that the decimal part 0.4 is equivalent to the fraction . We can use these conversions to calculate the modulus of not-too-huge numbers on a standard calculator.
๐ŸŒ
Omni Calculator
omnicalculator.com โ€บ math โ€บ modulo
Modulo Calculator
May 8, 2025 - To calculate modulo division: subtract the divisor from the dividend until the resultant is less than the divisor. The components of modulo division are dividend, divisor, quotient, and remainder.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70775296 โ€บ what-is-the-best-way-more-straight-forward-way-to-calculate-a-modulus-operatio
What is the best way, more straight-forward, way to calculate a modulus operation?
The modulus operator is just finding the remainder of division with numbers A and B. So, as you said, if you have a decimal, remove it and multiply those two numbers together, then subtract the result from the original dividend and the result ...
๐ŸŒ
HP Forums
hpmuseum.org โ€บ forum โ€บ thread-19367.html
Modulo or reminder computation on calculators without the modulo function
ยซ Previous 1 โ€ฆ 20 21 22 23 24 โ€ฆ 75 Next ยป Modulo or reminder computation on calculators without the modulo function
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Modulo
Modulo - Wikipedia
2 weeks ago - In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the modulus of the operation. Given two positive numbers a and n, a modulo n (often abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. For example, the expression "5 mod 2" evaluates to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0, because 9 divided by 3 has a quotient of 3 and a remainder of 0.
๐ŸŒ
MathCracker
mathcracker.com โ€บ modulo-calculator
Modulo Calculator - MathCracker.com
May 29, 2025 - The modulo operation, which is traditionally denoted by the symbol %, calculates the remainder after dividing the two numbers provided. For example, \(10 \mod 3\) equals 1 because 10 divided by 3 leaves a remainder of 1, since the highest integer ...
๐ŸŒ
Sivo
blog.sivo.it.com โ€บ home โ€บ modulus calculation โ€บ how to calculate modulus without a calculator?
How to Calculate Modulus Without a Calculator? | Modulus Calculation โ€“ Sivo
June 19, 2025 - According to the rule, If there is no decimal part, the MOD value is 0. Therefore, 10 mod 2 = 0. (Alternatively, using the formula: 10 - (2 ร— 5) = 10 - 10 = 0). ... By performing the division, finding the integer quotient (by removing decimals), and then using the remainder formula, you can ...
๐ŸŒ
CalculatorSoup
calculatorsoup.com โ€บ calculators โ€บ math โ€บ modulo-calculator.php
Modulo Calculator
The modulo operation finds the ... operation finds the remainder of a divided by b. To do this by hand just divide two numbers and note the remainder....