# 모듈로 계산기

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

직접 계산할 수 있는 페이지: https://www.calcopenly.com/ko/math/modulo-calculator
분야: 수학 계산기

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.

## 입력

- **Calculate** (선택 항목: Remainder a mod n, Power a^b mod m, Inverse a⁻¹ mod m)
- **Dividend a**: Integers, decimals or fractions.
- **Divisor n**
- **Base a**
- **Exponent b**: Any size, e.g. 10^18. A negative exponent uses the inverse of a.
- **Modulus m**
- **Number a**
- **Modulus m**

## 결과

- 결과 — 주요 결과
- Truncated remainder (sign of a)
- Euclidean remainder (never negative)
- Floored quotient ⌊a ÷ n⌋
- 정확한 값

## 공식

$$
\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}
$$

## 계산 예제

### −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

### 2^(10^18) mod 1 000 000 007

- Calculate: Power a^b mod m
- Base a: 2
- Exponent b: 10^18
- Modulus m: 1000000007
- **결과: 719,476,260**
- 검증 출처: Python 3.8 pow(2, 10**18, 10**9 + 7) (scratchpad forkA/verify_mod.py)

### RSA encryption 65^17 mod 3233

- Calculate: Power a^b mod m
- Base a: 65
- Exponent b: 17
- Modulus m: 3233
- **결과: 2,790**
- 검증 출처: Wikipedia — RSA (cryptosystem) worked example, c = 65^17 mod 3233 = 2790

## 자주 묻는 질문

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

## 출처

- 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](https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/)
- [Wikipedia — Modular exponentiation (4^13 mod 497 example)](https://en.wikipedia.org/wiki/Modular_exponentiation)
- [Microsoft Excel MOD function](https://support.microsoft.com/office/mod-function-9b6cd169-b6ee-406a-a97b-edf2a9dc24f3)
