Interviewer: write a code to find the greatest common divisor of two numbers


Recently, I went to an interview and met several companies. I deeply realized that the more basic the problem is, the more important it is. The more I can investigate a person’s technical skills and logical thinking. Like what we’re going to say nextFind the greatest common divisor of two numbersThe problem. This kind of simple algorithm question will appear in the interview link, the interviewer asks you to tear the kind on the spot.

To get to the point, first of all, we need to know that the interviewerFind the greatest common divisor of two numbersWhat is the purpose of this topic. If you know yourself and your enemy, you can be invincible in a hundred battles. The first thing to look at isThe candidate’s mathematical backgroundIt depends on whether you know some common divisor algorithms, such as round and round division, more relative subtraction, and so on. The second is to examine your code ability to see if you can write the algorithm in code, whether there are bugs in the code, and pay attention to the boundary treatment, etc. Let’s take a look at the performance of different candidates.

First, if you don’t have a solid foundation in mathematics and don’t know the existing methods for finding common divisor, you can only write the following code.

//Violence solution

This is the violent solution, so don’t explain it too much. The interviewer is obviously not satisfied with the code.

Second, we know some common divisor algorithms, but we can’t implement them in code. If we don’t have enough code, we can only go home and wait for notice.

Third, know the common divisor algorithm, and can write code. For example, tell the interviewer that you knowComparative reduction methodIt can be solved. The basic principle isSubtract the smaller number by the larger number, then compare the difference with the smaller number, and subtract the decimal by the large number. Continue this operation until the resulting subtraction and difference are equal.Generally, at this time, the interviewer will express his approval and let you implement the code. Then you will write the following code:

//Comparative reduction method

How about it? Is it very simple!

Of course, you can also do it by rolling and rolling. The basic principle isThe greatest common divisor of two integers is equal to the greatest common divisor of the smaller one and the divisor remainder of two integers。 The code is as follows:

//Division by rolling

In general, can say the most familiar method and give its specific implementation on it, this topic is basically passed.

Finally, there are many ways to find the greatest common divisor, such as prime factor decomposition, short division and so on. If you are interested, you can learn more and expand your own thinking.

If you think the article is useful,give the thumbs-up+followBai, so that more people can see this article, but also encourage bloggers to write more good articles. More aboutAlgorithm, data structure and computer basic knowledge“Welcome to the official account of my original public address.”Chaoyue programming」。

Interviewer: write a code to find the greatest common divisor of two numbers

Recommended reading

Why are there red and black trees? What is a red black tree? You will understand after reading this article

What are B-tree and B + tree in the series of data structure? Why can’t binary search trees work?

It’s 2020. I heard that you won’t merge and rank? Hand in hand teaching you handwritten merge sorting algorithm

Why multithreading? What is thread safety? How to ensure thread safety?

What is a heap in the series of data structure? It’s enough to read this one