Calculatrice de modulo (a mod n)

Cette calculatrice trouve a mod n, le reste de la division de a par n, pour tous les entiers, y compris négatifs. Elle montre le quotient, la soustraction et la différence entre la réponse mathématique et l’opérateur % de certains langages de programmation.

Résultat

Saisissez les valeurs et appuyez sur Calculer pour voir le résultat.

Formule

a mod n=a−n⌊an⌋a \bmod n = a - n \left\lfloor \frac{a}{n} \right\rfloor

Divide, round down to a whole number, multiply back and subtract. For n > 0 the answer is between 0 and n − 1.

Comment utiliser cette calculatrice

  1. Enter the number a and the modulus n (whole numbers; n cannot be 0).
  2. Press Calculate to see a mod n, the quotient and each step.

Exemples corrigés

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.

Erreurs fréquentes

  • 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.

Questions fréquentes

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.