JAVA uses toss and turns to find the greatest common divisor

background

The company purchased a gold brick, which was originally intended to be used as a floor mat at the door. But the boss of "Hou Lang" felt cheesy and prepared to switch to platinum bricks. So, it was sent to the employees to sell. However, the employees could not find a customer who could afford the BRIC, so the boss of Houlang decided to sell it separately:
chat record

Ask for help

This question baffled Xiao Han, and Xiao Han began to ask for help in the group:
New conversation

Ideas

This problem was instantly transformed into a problem of finding the greatest common divisor by the elementary school students, so Xiao Han began to look for the code brother to write a script:

Talk to code brother
The reason why the code brother is so confident is because he got a sentence from Baidu:

Theorem: The greatest common divisor of two positive integers is equal to the greatest common divisor of the remainder obtained by dividing the large number by the decimal number and the smaller number.
That is: set a>b, c = the greatest common divisor of a and b, then c = (a%b) and the greatest common divisor of b.

According to the code brother's intuition, this sentence itself is a recursive sentence:

c=the greatest common divisor of a and b; c=the greatest common divisor of b and (a%b) c=the greatest common divisor of (a%b) and b%(a%b).

for example:

If a is 12, b is 8, and c is the greatest common divisor: c=the greatest common divisor of 12 and 8; c=the greatest common divisor of 8 and 4; c=the greatest common divisor of 4 and 0;

But what is the greatest common divisor of 4 and 0?
In fact, 8%4 is equal to 0. When the remainder is zero, it can be proved that 4 is a factor of 8, and 4 is the greatest common divisor of 4 and 8.
Therefore, it can also be said that the greatest common divisor of 0 and any positive integer is the positive integer itself. I did not make up this theorem. The original words of Mr. Min Sihe in "Elementary Theory of Numbers" read:

Let b be any positive integer, then the common factor of 0 and b is the factor of b.

At this time, the recursive condition of the code brother has been completely found, and the two numbers are mutually calculated, and one of them is zero to exit the recursion. At this time, the other number is the greatest common factor sought.

Code

This method is called the Euclidean algorithm, also known as the tossing and dividing method.

public class Main {
    
    
	
	public static int getGreatestCommomDivisor(int x,int y)
	{
    
    
		if(x==0)
		{
    
    
			return  y;
		}
		int big = x>y?x:y;
		int small = x>y?y:x;
		return getGreatestCommomDivisor(big%small,small);
	}

    public static void main(String[] args) throws Exception {
    
    
    	 System.out.println(getGreatestCommomDivisor(12,8));
    }
}

Of course, you can also write in a loop:

public static int getGreatestCommomDivisor(int x,int y)
	{
    
    
		int big = x>y?x:y;
		int small = x>y?y:x;
		while(small!=0) {
    
    
			int temp = small;
			small = big%small;
			big = temp;
		}
		return big;
	}

    public static void main(String[] args) throws Exception {
    
    
    	 System.out.println(getGreatestCommomDivisor(111,18));
    }

Follow-up

Xiaohan was promoted and raised, and lived a happy life with Xiaogang. Brother Ma had nothing, and quietly read "Self-cultivation of a Spare Tire". This story tells us_
ending

Guess you like

Origin blog.csdn.net/weixin_44159662/article/details/106361583