剰余計算機(モジュロ)

Calculate a mod n under the floored, truncated and Euclidean conventions, modular powers a^b mod m of large numbers, and modular inverses, with steps.

更新日 検証済みの例:10

Integers, decimals or fractions.
試す
結果
結果: 3
小数点以下の最大桁数:12;最も近い値へ、等距離ならゼロから遠い値へ
Truncated remainder (sign of a)
−2
Euclidean remainder (never negative)
3
Floored quotient ⌊a ÷ n⌋
−4

−17 ÷ 5 leaves 3 when the quotient is rounded down to −4 (Python, Excel MOD), but −2 when it is rounded toward zero to −3 (C, JavaScript %).

Where a sits between multiples of n

−20−15a = −17floored r = 3truncated r = −2
計算方法 S
  1. Divide

    an=−175=−175≈−3.4\frac{a}{n} = \frac{-17}{5} = -\frac{17}{5} \approx -3.4
  2. Floored remainder (sign of the divisor)

    q=⌊−175⌋=−4,r=a−nq=−17−5⋅(−4)=3q = \left\lfloor -\frac{17}{5} \right\rfloor = -4,\quad r = a - n q = -17 - 5 \cdot \left(-4\right) = 3

    Python's %, Excel's MOD and most maths texts round the quotient down.

  3. Truncated remainder (sign of the dividend)

    q=trunc⁡(−175)=−3,r=−17−5⋅(−3)=−2q = \operatorname{trunc}\left(-\frac{17}{5}\right) = -3,\quad r = -17 - 5 \cdot \left(-3\right) = -2

    C, Java and JavaScript's % round the quotient toward zero.

  4. Euclidean remainder (never negative)

    r=a−∣n∣⌊a∣n∣⌋=3r = a - |n| \left\lfloor \frac{a}{|n|} \right\rfloor = 3

剰余計算機(モジュロ)について

The modulo operation a mod n gives the remainder left when a is divided by n. For positive numbers every convention agrees (17 mod 5 = 2), but for negative numbers they split: the floored remainder r = a − n⌊a/n⌋ takes the sign of the divisor, the truncated remainder rounds the quotient toward zero and takes the sign of the dividend, and the Euclidean remainder is never negative. The calculator shows all three, and also computes modular powers by square-and-multiply and modular inverses by the extended Euclidean algorithm.

Clock and calendar arithmetic, hashing and cyclic buffers all use remainders, and cryptography rests on modular powers. The default, −17 mod 5, is 3 under floored division (Python's %, Excel's MOD) but −2 under truncated division (the % of C and JavaScript). The textbook RSA example encrypts 65 as 65^17 mod 3233 = 2790.

Modular powers stay exact for exponents as large as 10^18 because every squaring is reduced mod m. An inverse a⁻¹ mod m exists only when gcd(a, m) = 1.

計算例

−17 mod 5

Calculate
Remainder a mod n
Dividend a
-17
Divisor n
5
結果
3
Truncated remainder (sign of a)
-2
Euclidean remainder (never negative)
3
Floored quotient ⌊a ÷ n⌋
-4

照合元:Python 3.8: -17 % 5 = 3, math.fmod(-17, 5) = -2.0, -17 // 5 = -4

17 mod −5

Calculate
Remainder a mod n
Dividend a
17
Divisor n
-5
結果
-3
Truncated remainder (sign of a)
2
Euclidean remainder (never negative)
2

照合元:Python 3.8: 17 % -5 = -3, math.fmod(17, -5) = 2.0; Euclidean 17 − 5·⌊17/5⌋ = 2

7.5 mod 2

Calculate
Remainder a mod n
Dividend a
7.5
Divisor n
2
結果
1.5
Truncated remainder (sign of a)
1.5

照合元:Python 3.8: 7.5 % 2 = 1.5

4^13 mod 497

Calculate
Power a^b mod m
Base a
4
Exponent b
13
Modulus m
497
結果
445

照合元:Wikipedia — Modular exponentiation worked example; Python pow(4, 13, 497) = 445

よくある質問

How do you calculate a mod n?

Divide, round the quotient down, and subtract: a mod n = a − n⌊a/n⌋. For 17 mod 5, 17 ÷ 5 = 3.4, which rounds down to 3, and 17 − 5 × 3 = 2. For −17 mod 5, −3.4 rounds down to −4, and −17 − 5 × (−4) = 3. On a 12-hour clock, 15:00 is 15 mod 12 = 3 o'clock.

Why do Python and JavaScript give different answers for a negative modulo?

They round the quotient differently. Python's % floors it, so −17 % 5 = 3, with the sign of the divisor; JavaScript, C and Java truncate toward zero, so −17 % 5 = −2, with the sign of the dividend. Both satisfy a = n × q + r. Excel's MOD matches Python. In JavaScript, ((a % n) + n) % n gives the floored answer when n is positive.

How do you calculate large powers modulo a number?

Use square-and-multiply: write the exponent in binary, square repeatedly, and reduce mod m after every step so the numbers never grow past m². For 4^13 mod 497, 13 is 1101 in binary and the answer is 445, the same as Python's pow(4, 13, 497). Computing 4^13 = 67,108,864 first works here, but not for exponents like 10^18.

What is a modular inverse?

The inverse of a modulo m is the number x with a × x ≡ 1 (mod m). 3⁻¹ mod 11 = 4 because 3 × 4 = 12 = 11 + 1. It exists only when gcd(a, m) = 1, so 2 has no inverse mod 10. The extended Euclidean algorithm finds it; in the textbook RSA example the private key 2753 is the inverse of 17 mod 3120, since 17 × 2753 = 46,801 = 15 × 3120 + 1.

What is the difference between remainder and modulo?

For positive numbers they agree: 17 divided by 5 leaves 2 either way. For negative numbers the remainder in the C and JavaScript sense follows the sign of the dividend (−17 rem 5 = −2), while modulo in the mathematical sense follows the divisor or is never negative (−17 mod 5 = 3). When the two differ, they differ by exactly |n|.

「剰余計算機(モジュロ)」の精度はどのくらいですか?

精度は入力値と計算方法の前提に依存します。十進演算には有効数字50桁を使いますが、推定、数値計算手法、元データの精度はそれより低い場合があります。表示の丸め処理でこれらの制約がなくなるわけではありません。 独立した出典の解答と照合した計算例:10。 例えば、「−17 mod 5」はPython 3.8: -17 % 5 = 3, math.fmod(-17, 5) = -2.0, -17 // 5 = -4と照合しています。

この計算方法の出典は何ですか?

Knuth, The Art of Computer Programming Vol. 1, §1.2.4 (mod) and Vol. 2, §4.6.3 (powers); Leijen (2001), Division and modulus for computer scientists; Wikipedia — Modular exponentiation (4^13 mod 497 example); Microsoft Excel MOD function.

この計算機について

a mod n=a−n⌊an⌋ab mod m by square-and-multiplya a−1≡1(modm)\begin{gathered} a \bmod n = a - n\left\lfloor \frac{a}{n} \right\rfloor \\[6pt] a^{b} \bmod m \text{ by square-and-multiply} \\[6pt] a\,a^{-1} \equiv 1 \pmod m \end{gathered}

出典

  1. Knuth, The Art of Computer Programming Vol. 1, §1.2.4 (mod) and Vol. 2, §4.6.3 (powers)
  2. Leijen (2001), Division and modulus for computer scientists
  3. Wikipedia — Modular exponentiation (4^13 mod 497 example)
  4. Microsoft Excel MOD function

出典と照合済み

この計算機には、独立した出典の解答を使った計算例が 10 件あります。テストに組み込まれており、ここでも実行できます。

関連する計算ツール