Euclidean realization of the principle of solving the greatest common divisor

package Test_1;
import java.util.Scanner;

public class Test_6 {

	/**
	 * @param args
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
		System.out.print("请输入两个要求公约数的整数:");
		Scanner input = new Scanner(System.in);
		int n1 = input.nextInt();
		int n2 = input.nextInt();
		int s = division(n1,n2);
		System.out.println(n1+"与"+n2+"的最大公约数为:"+s);
		//最小公倍数=两数乘积/最大公约数
		System.out.println(n1+"与"+n2+"的最小公倍数为:"+n1*n2/s);
		
		
	}

	private static int division(int n1, int n2) {
		// TODO Auto-generated method stub
		/*
		 * 此时并不区分n1与n2的大小,虽然求解过程是按照n2<n1来计算的,
		 * 因为当n1<n2时,n1%n2的值为n1,然后再执行division(n2,n1),
		 * 相当于把值交换了,有相当于求(大值,小值),因此需要讨论n1与n2的大小
		 * */
		if (n2 == 0) return n1;
		return division(n2,n1%n2);
		
		
		
	}


}

The greatest common divisor of two integers is divisible their maximum positive integer simultaneously. Euclidean algorithm based on the principle: the greatest common divisor of two integers wherein a greatest common divisor equal to a small number and the remainder divided by the number of the two numbers


Realization of java




Published 69 original articles · won praise 16 · views 220 000 +

Guess you like

Origin blog.csdn.net/huanglei1234567890/article/details/9231627