Why does the chinese remainder theorem work




















Chinese Remainder Theorem states that there always exists an x that satisfies given congruences. Below is theorem statement adapted from wikipedia. Let num[0], num[1], …num[k-1] be positive integers that are pairwise coprime.

Then, for any given sequence of integers rem[0], rem[1], … rem[k-1], there exists an integer x solving the following system of simultaneous congruences.

The first part is clear that there exists an x. The second part basically states that all solutions including the minimum one produce the same remainder when divided by-product of n[0], num[1],.. And 11 is one solution, other solutions are 71, ,.. All these solutions produce the same remainder when divided by 60, i. A Naive Approach to find x is to start with 1 and one by one increment it and check if dividing it with given elements in num[] produces corresponding remainders in rem[].

Once we find such an x, we return it. Below is the implementation of Naive Approach. Length; Console. Auxiliary Space : O 1 See below link for an efficient method to find x.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Let's first introduce some notation, so that we don't have to keep writing "leaves a remainder of You can read more about this in the NRICH article on Modular Arithmetic , but you don't need to have read that article in order to understand this article.

Of course, there are lots of numbers that all correspond to the same point. You might like to think about why this is - we'll return to this soon. Let's pause for a moment. Check this yourself - it's not too hard. You should check these statements yourself, using the definition of what it means for a number to correspond to a point.

In fact, this correspondence goes further than a simple relabelling. This is often cheaper because for many algorithms, doubling the size of the input more than doubles the running time. Proof : This is an easy induction from the previous form of the theorem, or we can write down the solution directly.



0コメント

  • 1000 / 1000