Examples of Chinese Remainder Theorem algorithm implemented by Python and python theorem Algorithm

Source: Internet
Author: User

Examples of Chinese Remainder Theorem algorithm implemented by Python and python theorem Algorithm

This example describes the Chinese Remainder Theorem algorithm implemented by Python. We will share this with you for your reference. The details are as follows:

Chinese Remainder Theorem-CRT: Also known as Sun Tzu's Theorem, It is a Theorem in number theory. That is, if a person knows the remainder obtained by dividing a number n by multiple integers, when these divisor is mutually qualitative, this person can uniquely determine the remainder obtained by dividing n by the product of these integers.

Wiki on Wikipedia: The Chinese remainder theorem is a theorem of number theory, which states that, if one knows the remainders of the division of an integer n by several integers, then one can determine uniquely the remainder of the division of n by the product of these integers, under the condition that the divisors are pairwise coprime.

There is a number of n, divided by 2, divided by 1, divided by 3, divided by 2, divided by 5, divided by 6, divided by 5, and is exactly divided by 7, calculate the number n.

Analysis: n is divided by 2 and the remainder 1 indicates that the minimum value is 1. After that, the condition is always satisfied, so the number to be added must be a multiple of 2. Divide 3 into 2, that is, (1 + 2 * I) % 3 = 2, where I is a positive integer. This condition is always met, so the number to be added must be a multiple of 3. Because of the limitation of the previous condition, it is an integer multiple of the minimum public multiples of 2 and 3. And so on, you can find the number divisible by 7.

N = 1 while (n % 3! = 2): n + = 2 while (n % 5! = 4): n + = 6 while (n % 6! = 5): n + = 30 while (n % 7! = 0): n + = 30

The result is 119.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.