How to Use Python to solve the maximum Common Divisor

Source: Internet
Author: User
This article describes how to use Python to solve the maximum common divisor, including how to use Python to represent the solutions of Euclidean and Stein algorithms. For more information, see 1. Euclidean Algorithm

The euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers a and B. The computation principle depends on the following theorem:
Theorem: gcd (a, B) = gcd (B, a mod B)

Proof:
A can be expressed as a = kb + r, then r = a mod B
Assume that d is a common divisor of a and B, there is d | a, d | B, and r = a-kb, so d | r.
Therefore, d is the common divisor of (B, a mod B.
If d is the common divisor of (B, a mod B), d | B, d | r, but a = kb + r, d is also the common divisor of (a, B.
Therefore, (a, B) and (a, a mod B) have the same common number, and the maximum common number must be equal.

The Python language of Euclidean is described as follows:

def gcd(a, b): if a < b:  a, b = b, a while b != 0:  temp = a % b  a = b  b = temp return a

2. Stein Algorithm
The euclidean algorithm is a traditional algorithm used to calculate the maximum common divisor of two numbers. It is both theoretical and efficient. However, he has a fatal defect that is only apparent when a prime number is large.
Consider the current hardware platform. Generally, an integer can be up to 64 bits. For such an integer, it is very easy to calculate two values. For a 32-bit platform, to compute two integers of no more than 32 bits, only one instruction cycle is required. To compute an integer model of no more than 64 bits, there are only a few cycles. However, for a larger prime number, this calculation process has to be designed by the user. To calculate two integers that exceed 64 bits, users may have to adopt trial and commercial law similar to the multi-bit Division manual calculation process. This process is not only complex, but also consumes a lot of CPU time. Modern cryptographic algorithms require the calculation of prime numbers of more than 128 bits. The design of such a program is eager to discard division and modulo.
The Stein algorithm was proposed by J. Stein in 1961. This method is also used to calculate the maximum common divisor of two numbers. Unlike the Euclidean algorithm, the Stein algorithm only has integer shift and addition and subtraction, which is a good news for programmers.
To illustrate the correctness of the Stein algorithm, we must first note the following conclusions:
Gcd (a, a) = a, that is, a number and its own common number is its own.
Gcd (ka, kb) = k * gcd (a, B), that is, the maximum common approx. Operation and multiplication operation can be exchanged. In special cases, when k is 2, it indicates that the maximum number of two even values, for example, can be divisible by 2.
The python implementation of the Stein algorithm is as follows:

def gcd_Stein(a, b):    if a < b:    a, b = b, a  if (0 == b):    return a  if a % 2 == 0 and b % 2 == 0:    return 2 * gcd_Stein(a/2, b/2)  if a % 2 == 0:    return gcd_Stein(a / 2, b)  if b % 2 == 0:    return gcd_Stein(a, b / 2)    return gcd_Stein((a + b) / 2, (a - b) / 2) 

3. General Solution implementation

The core code is simple:

def gcd(a, b):if b == 0:return areturn gcd(b, a % b)

The following is a general method to evaluate the maximum common divisor using Python and determine whether it is a prime number:
The procedure is as follows:

#! /Usr/bin/env python def showMaxFactor (num): count = num/2 while count> 1: if num % count = 0: print 'largest factor of % d is % d' % (num, count) break # When the break jumps out, the following else statement count-= 1 else: print num appears, "is prime" for eachNum in range (10, 21): showMaxFactor (eachNum)

The output is as follows:

largest factor of 10 is 511 is primelargest factor of 12 is 613 is primelargest factor of 14 is 7largest factor of 15 is 5largest factor of 16 is 817 is primelargest factor of 18 is 919 is primelargest factor of 20 is 10

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.