Python implementation for greatest common divisor and least common multiple

Source: Internet
Author: User
Tags greatest common divisor

Record Python implementation of greatest common divisor & minimum number of public digits two algorithms

Concept

Greatest common divisor: refers to the largest of two or more integers in total

Least common multiple: two or more integers of the public are called their common multiple, where the smallest common multiple other than 0 is called the least common multiple of these integers.

The relationship between the two: the product of two numbers = least common multiple * greatest common divisor

Instance

Euclidean method

A=int (Input (' Please enter 1st num: ') "B=int (Input (' Please enter 2nd num: ') s=a*bwhile a%b!=0:    a,b=b, (a%b)   Else :    print (b, ' was the maximum common divisor ')    print (s//b, ' is the least common multiple ') #运行结果please enter 1st Num:10 Please enter 2nd num:155 are the Maximum common Divisor30 is the Least common multiple

The method of more subtractive loss

A=int (Input (' Please enter 1st num: ') b=int (input (' "Please enter 2nd num: ')") s=a*b while    a!=b:    if a>b:        a-= b    elif a<b:        b-=aelse: Print (    A, ' was the maximum common divisor ')    print (s//a, ' is the least common Multiple ') #运行结果please enter 1st Num:40please Enter 2nd num:6020 is the maximum common divisor120 is the least common multi Ple

  

The difference between the division method and the more subtractive loss (1) is the method to find the maximal common factor, the calculation of the divided method to divide-based, the more subtractive loss of the main subtraction, the calculation of the number of divided method is relatively small number of calculations, especially when the number of two different size difference is more obvious. (2) from the manifestation of the results, the result of the method is that the dividing remainder is 0, and the subtraction is obtained by the same difference between the meiosis and the differential.

Python implementation for greatest common divisor and least common multiple

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.