How do you solve problems with the Chinese remainder of the sentence
Mr. A. celebrated a milestone birthday this year; at the same time he has also completed a full seven-year period. How old did Mr. A. get? The answer - 70 years old - is not difficult to guess. Mr L., on the other hand, completed the last full year seven two years ago; his last big birthday was already 8 years ago. How old is Mr. L.?
It is interesting that the age of Mr. L. is actually clearly determined by these two details, at least if one assumes a realistic age of a person, namely years.
The number results in the remainder 2 if the whole number is divided by 7 and the remainder 8 if the whole number division by 10 is used. Which number is?
So the number can be represented as
= ·7 + 2 = ·10 + 8
or in general
= · + = · +
In other words, the following applies
The numbers and are referred to as modules in this context, the numbers and as the associated remainders.
The so-called Chinese remainder of the law states that if the modules and are relatively prime, there is modulo · a unique solution.
Using the Chinese remainder, calculations can be made in based on calculations in 0 × ... × -1, in which 0, ..., -1 are the prime powers of.
Since and are coprime, the greatest common factor 1 can be represented as
1 = · + ·
The coefficients and are not clearly determined here, but there are many values for and that satisfy the equation. The extended Euclidean algorithm calculates the greatest common divisor and a possible value for and from and.
Multiplication with (-) gives
- = (-)·· + (-)··
(-)·· + = -(-)·· +
The searched coefficients and for and are found. So is
= (-)·· +
a possible solution.
However, we are looking for the unambiguous solution modulo ·. To calculate the value of modulo · it is sufficient to reduce the product (-) · modulo, because it is
(-) mod +
<(-) mod + (da <)
= ((-) mod + 1)
((-1) + 1 ) ·
= (-) mod +
the uniquely determined number sought.
The Chinese remainder theorem can be formulated in general for coprime modules and associated remainders.
Sentence: (Chinese remainder sentence)
Given are coprime modules 0, ..., -1 and associated remnants 0, ..., -1. The number that gives the remainder modulo is uniquely determined modulo the product of all.
The following recursive function chineseRemainder receives a list of modules and a list of associated remainders as parameters. If these lists consist of only one item at a time, the function returns those items. Otherwise it recursively first calculates the number modulo, which results from the first half of and according to the Chinese remainder, and then the number modulo, which results from the second half of and. The products and are coprime, since they are all coprime to one another. The value is given by the function extgcd calculated using the extended Euclidean algorithm; the other two calculated values and are not used. From and as well as the associated residues and the solution can then be calculated using the method given above. In addition to this solution, the function also returns the associated module.
The implementation in the Python programming language follows. Again, use is made of the option of assigning tuple values. The notation nn [: k] denotes an excerpt (slice) from the list nn from start to index (only). Similarly, nn [k:] denotes a section from the index (inclusive) to the end of the list.
|Input:||List of coprime modules, list of remainders|
|Output:||Product of the modules, number according to the Chinese remainder theorem|
The advantage of this implementation based on the divide-and-conquer principle is that in the lower recursion levels there are many calculations with small numbers and only a few calculations with large numbers in the upper recursion levels.
Implementation in Haskell
One possible implementation in the functional programming language Haskell is given below. The parameters of the function are in turn a list of modules and a list of associated remainders. If these lists only consist of one element or one element, (,) is returned. Otherwise, the calculation is recursive according to the procedure given above.
|- Chinese remainder sentence chineseRemainder :: [Integer] -> [Integer] -> (Integer, Integer) chineseRemainder [n] [r] = (n, r) chineseRemainder nn rr = (m * n, x) where k = length nn `div` 2 (m, a) = chineseRemainder (take k nn) (take k rr) (n, b) = chineseRemainder (drop k nn) (drop k rr) (g, u, v) = extgcd mnx = (ba) * u `mod` n * m + a|
The function extgcd performs the calculation of the extended Euclidean algorithm.
On the demo
If we line up in rows of ten, one is not enough. If we line up in rows of nine, one is also too few. It continues like this up to rows of two, where one is also missing. How much are we? (Under 3000).
Note: When using the Chinese remainder theorem, the modules must be coprime.
In this case, the solution is even easier. If the remainder are all equal, the solution is the least common multiple (lcm) of the modules plus this remainder. This remainder is -1 here.
|[AHU 74]||A.V. Aho, J.E. Hopcroft, J.D. Ullman: The Design and Analysis of Computer Algorithms. Addison-Wesley (1974)|
|[CLRS 01]||T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein: Introduction to Algorithms. 2nd edition, The MIT Press (2001)|
|[Lan 12]||H.W. Lang: Algorithms in Java. 3rd edition, Oldenbourg (2012) |
|[Lan 18]||H.W. Lang: Cryptography for Dummies. Wiley (2018) |
- How can you increase your self-worth?
- Lightning can damage your eyesight
- What is magnesium sulfur
- How does the software architecture influence the software quality
- How powerful is Iceland
- How valuable is the SQL certificate
- How many sixes are hits from Rohit
- What is your favorite light summer jacket
- What does Gall Karda mean in Punjabi
- What is the IUPAC of CH3OOCH3
- How Unacademy Makes Money With Ads
- Why does wind cause delays at airports
- Checks Chicago OH for warrants
- What do you expect from the catering service
- What are some postmodern reviews of postmodernism
- If a relay needs a fuse
- What are your standards for dating
- Which foods usually cause stomach distension
- Where are data structures actually used?
- Can catchy tunes pinch you with their butts
- How does 1 1 2 prove that
- Where would you recommend that I travel?
- What is a notch filter 1
- Can psychopaths have affection for animals?