Implementation of 001-Euclidean algorithm

Source: Internet
Author: User
Tags greatest common divisor

 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

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.