2015 Machine Test--Calculate the greatest common divisor and least common multiple of two positive integers

Source: Internet
Author: User
Tags greatest common divisor



Title Description:



Interface description



Prototype:



Long Getmaxdivisor (long lfirstinput, long lsecondinput);



Input parameters:



int First: integer number one;



int second: second integer;



return value:



Greatest common divisor



Long Getminmultiple (long lfirstinput, long lsecondinput);



Input parameters:



int First: integer number one;



int second: second integer;



return value:



Least common multiple






Problem-solving ideas: Using the greatest common divisor method can be obtained, two number of least common multiple = two number product/two number of greatest common divisor






Euclidean method:


... R1 (0≤R1). If the r1=0, then (A, B) =b, if r1≠0, then the second divided by the R1, B÷r1=q ... r2  (0≤R2). If r2=0, then (A, B) =r1, if r2≠0, continue to use R1 in addition to R2, ... So go on until you can divide it evenly. The divisor of the last remainder of the dividend is (a, b). For example: A=25,b=15,a/b=1 ... 10,b/10=1 ...... 5,10/5=2 ....... 0, the last one is the divisor of the remainder of the divisor is 5,5 is to seek greatest common divisor. The code is as follows:
    public static long Getmaxdivisor (long lfirstinput, long Lsecondinput)    {        long mul,temp;        Mul=lfirstinput*lsecondinput;        while (lfirstinput!=0) {temp=lfirstinput;lfirstinput=lsecondinput%lfirstinput;lsecondinput=temp;}        return lsecondinput;    }        function: Get two integers of least common multiple    //input: Two integers    //return: least common multiple public    static long getminmultiple (long lfirstinput, long Lsecondinput)    {        long mul,temp;        Mul=lfirstinput*lsecondinput;        while (lfirstinput!=0) {temp=lfirstinput;lfirstinput=lsecondinput%lfirstinput;lsecondinput=temp;}         return mul/lsecondinput;    }







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



2015 Machine Test--Calculate the greatest common divisor and least common multiple of two positive integers


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.