剰余の計算機 (a mod n)
この計算機は a mod n、つまり a を n で割った余りを、負の数を含むどんな整数についても求めます。商、引き算、そして数学の答えと一部のプログラミング言語の % 演算子との違いを示します。
結果
値を入力して「計算」を押すと結果が表示されます。
公式
Divide, round down to a whole number, multiply back and subtract. For n > 0 the answer is between 0 and n − 1.
この計算ツールの使い方
- Enter the number a and the modulus n (whole numbers; n cannot be 0).
- Press Calculate to see a mod n, the quotient and each step.
解き方付き例題
17 mod 5
17 ÷ 5 = 3.4, which rounds down to 3.
17 − 5 × 3 = 2, so 17 mod 5 = 2.
−17 mod 5 (a negative number)
−17 ÷ 5 = −3.4, which rounds down to −4.
−17 − 5 × (−4) = 3, so −17 mod 5 = 3. (The % operator in C or JavaScript gives −2.)
100 mod 7
100 ÷ 7 = 14.28…, so the quotient is 14.
100 − 7 × 14 = 2.
よくある間違い
- Giving the quotient instead of the remainder: 17 mod 5 is 2, not 3.
- Rounding −3.4 to −3 instead of down to −4 for negative numbers.
- Expecting the same answer from % in every programming language; they differ for negatives.
よくある質問
What does mod mean in math?
a mod n is the remainder when a is divided by n. For example 17 mod 5 = 2, because 17 = 3 × 5 + 2.
How do you calculate mod of a negative number?
Divide and round down (towards negative infinity), then subtract. −17 mod 5: −17 ÷ 5 = −3.4 → −4, and −17 − 5 × (−4) = 3.
Where is modulo used?
Clock arithmetic (14:00 is 2 pm because 14 mod 12 = 2), checking whether a number is even (n mod 2), check digits, and programming.