From the wrong Middle School python (4)--minimum number of conventions and the method of dividing

Source: Internet
Author: User
Tags gcd greatest common divisor

Topic

Give you two positive integers a and B, and output their greatest common divisor.

The steps of the method of dividing the Euclidean method
def gcd(b,a):    b,a=a,b%a    if a==0:        return b    else:        return gcd(b,a)

That is, if B and a cannot divide evenly, then it is a recursive idea to take the remainder of a and b divided by a.

Understand

It is possible to understand the method of dividing by two angles.

1. Example method

A rectangular paper with a length of 2703 cm and a width of 1113 cm. To cut it into several squares of the same size, the paper must be as large as possible, and the side length of the square should be as big as it can be. Q: How many centimeters is the side length of such a square?
Answer:

Know: In the length of 2703 cm, width 1113 centimeters of rectangular paper one end, and then cut to width (1113 cm) for the side of the Square 2. After the cut is 1113 cm long, 477 cm wide rectangle, and then cut to width (477 cm) for the side of the Square 2. And then in the left rectangle (length 477 cm, Width 159 cm), 159 cm for the edge of the long cut square, just cut into 3, and no surplus. It is therefore known that 159 centimeters are 477 centimeters, 1113 centimeters and 2703 centimeters in approximate. So the side length of the square with the same size and length as long as possible should be 159 centimeters. So, 159 centimeters is the greatest common divisor of 2703 and 1113.
Let's turn the process into a computational process, namely:
2703÷1113, 2 more than 477;
1113÷477, 2 more than 159;
477÷159, more than 3 merchants 0.

When the remainder is 0 o'clock, the divisor 159 of the last calculation is the original two greatest common divisor of 2703 and 1113.
Visible, 477=159X3,
1113=159X3X2+159=159X7,
2703=159x7x2+477=159x7x2+159x3=159x17.
and ∵7 and 17 are Inma,
∴159 are the greatest common divisor of 2703 and 1113.
We call this method of greatest common divisor. The advantage of the Euclidean method is that it can find out the greatest common divisor of any two numbers in a short time.

2. Formula Understanding method

B=ak+r; If a number can be divisible by a and r, it must be divisible by B.

Python solution
def gcd(b,a):    b,a=a,b%a    if a==0:        return b    else:        return gcd(b,a)print(gcd(a,b))

This solution has been very long, but there are solutions to judge A and b who big who small, in fact, is not necessary. Because it is the same after the remainder is taken. Here the name of our article is called from the wrong high school python, not that the solution is wrong, but that is not concise. Does not reflect the superiority of python.

Cyclic method

Here we use loops to replace the previous recursion

while(b%a!=0):    a,b=b%a,aprint(a)

That's a lot simpler.

List method

There is only one line of code, using the Max () function; I think it is relatively concise, but inefficient. High complexity of time.

forin range(1,b+1if b%i==0and a%i==0]))

Copyright NOTICE: This article for Bo Master original article, welcome to reprint and share, but please declare the source http://blog.csdn.net/zhzz2012

From the wrong Middle School python (4)--minimum number of conventions and the method of dividing

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.