PackageEuclidean_algorithm;ImportJava.util.Scanner;/** * @authorAlazy_cat * Euclidean algorithm Natural Language Description: * Calculates the greatest common divisor of two nonnegative integers x and y: if y = 0, then greatest common divisor is x; otherwise remainder = x y,x and y greatest common divisor are the largest of Y and remainder Number of Conventions*/ Public classEuclideanalgorithm { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.print ("Please enter two integers:"); intx = 0, y = 0; X=In.nextint (); Y=In.nextint (); System.out.println ("X, Y's greatest common divisor is:" + euclideanalgorithm_01 (x, Y, 1)); System.out.println ("---------------------"); System.out.println ("X, Y's greatest common divisor is:" + euclideanalgorithm_02 (x, Y, 1)); } //recursive implementation of Euclidean algorithm Public Static intEUCLIDEANALGORITHM_01 (intXintYintcount) { //when y = 0 o'clock, recursion ends intremainder = 0; System.out.println ("First" + count++ + "secondary recursion:" + "x =" + x + "," + "y =" +y); if(y = = 0) returnx; Remainder= x%y; returneuclideanalgorithm_01 (y, remainder, count); } //loop implementation of Euclidean algorithm Public Static intEuclideanalgorithm_02 (intXintYintcount) { intremainder = 0; while(Y! = 0) {System.out.println ("First" + count++ + "secondary cycle:" + "x =" + x + "," + "y =" +y); Remainder= x%y; X=y; Y=remainder; } System.out.println ("First" + count++ + "secondary cycle:" + "x =" + x + "," + "y =" +y); returnx; }}
Implementation of 001-Euclidean algorithm