# Chinese Remainder Theorem (CRT)

Given $$N = \prod\_{i=1}^{k}n\_i$$ where each moduli(or divisor) $$n\_i$$ is a **pairwise** [**coprime**](https://en.wikipedia.org/wiki/Coprime_integers), and the set of $$a\_i$$ is given as below:

$$
x \equiv a\_1 \pmod {n\_1} \ \vdots \ x \equiv a\_k \pmod {n\_k}
$$

Then the system has a unique solution $$x$$ under modulo $$N$$ such that:

$$
x \equiv s\_1 \cdot \prod\_{i \ne 1} n\_i + s\_2 \cdot \prod\_{i \ne 2} n\_i + \cdots + s\_k \cdot \prod\_{i \ne k} n\_i \pmod N
$$

where each $$s\_i$$ satisfies:

$$
s\_i \cdot \prod\_{j \ne i} n\_j = a\_i \pmod {n\_i}
$$

In other words, a set of modulus statements can be reduced to a single statement.

Take the example below:

$$
x = \begin{cases} 2 \pmod 3 \ 3 \pmod 5 \ 2 \pmod 7 \end{cases}
$$

$$
35 \cdot s\_1 \equiv 2 \cdot s\_1 \equiv 2 \pmod 3 \ 21 \cdot s\_2 \equiv s\_2 \equiv 3 \pmod 5 \ 15 \cdot s\_3 \equiv s\_3 \equiv 2 \pmod 7 \\
$$

Solving these gives $$s\_1 = 1, s\_2 = 3$$ and $$s\_3 = 2$$. Then the solution is

$$
x \equiv 1 \cdot 35 + 3 \cdot 21 + 2 \cdot 15 \equiv 128 \equiv 23 \pmod {105}
$$

> Written by [Ryan Kim](mailto:undefined) of Fractalyze


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fractalyze.gitbook.io/intro/primitives/chinese-remainder-theorem-crt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
