How to solve greatest common divisor using Python

Source: Internet
Author: User
Tags greatest common divisor
1. Euclidean algorithm

Euclidean algorithm, also known as the greatest common divisor, is used to calculate two integers a, b of the two-way. Its computational principle relies on the following theorem:
Theorem: gcd (A, b) = gcd (b, a mod b)

Prove:
A can be expressed as A = kb + R, then r = a mod b
Suppose D is a, b of a convention number, then there is D|a, d|b, and r = a-kb, so d|r.
Therefore, D is the number of conventions (b, a mod b).
Plus d is the number of conventions (B,a mod b), then d|b, d|r, but a = kb + R, so d is also the number of conventions (a, B).
Thus, the number of conventions (a, B) and (A, a mod b) are the same, and their greatest common divisor are necessarily equally, to be proven.

Euclid's Python language is described as:

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
Euclidean algorithm is a traditional algorithm for calculating two numbers of greatest common divisor, which is very good both in theory and in efficiency. But he has a fatal flaw, and this flaw will only manifest in a large number of primes.
Consider the current hardware platform, the general integer is at most 64 bits, for such an integer, the calculation of two numerical values is very simple. For a platform with a word length of 32 bits, the modulo of two integers with no more than 32 bits is required, only one instruction period is needed, and the integer modulus of 64 bits or less is calculated, but only a few cycles. But for larger prime numbers, such a process has to be designed by the user, in order to calculate the two more than 64-bit integer modulo, the user may have to use similar to the multi-digit division hand calculation process of the trial law, the process is not only complex, and consumes a lot of CPU time. For modern cryptographic algorithms, the need to calculate more than 128 prime numbers abound, the design of such a program is eager to abandon division and modulo.
The Stein algorithm was proposed by J.stein in 1961, and this method is also a greatest common divisor for calculating two numbers. Unlike Euclid's algorithm, the Stein algorithm only shifts and adds and subtracts integers, which is a boon for program designers.
To illustrate the correctness of the Stein algorithm, we must first note the following conclusions:
GCD (A, a) = A, that is, a number and his own number of conventions is its own.
GCD (ka, KB) = k * GCD (A, b), that is, greatest common divisor operations and multiplier operations can be exchanged, special, when k=2, indicating that two even-numbered greatest common divisor such as 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)    

3. General Solution Implementation

The core code is simple:

Def gcd (A, B): if b = = 0:return Areturn gcd (b, a% b)

Enclose a general method for finding greatest common divisor in Python and determining 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 '%d '% (num, count)       break    #break跳出时会跳出下面的else语句     count-= 1   else:     pri NT NUM, ' is prime ' for  Eachnum in range (10,21):   
  

The output is as follows:

Largest factor of are 817 is primelargest factor of are 919 is primelargest factor of 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.