To seek greatest common divisor--[js practice by dividing the law

Source: Internet
Author: User
Tags gcd greatest common divisor

Topic Background Approximate

If a number a can be divisible by a number B, a is called a multiple of B, and B is called a 约数 .

Greatest common divisor

Greatest common divisor is a two-digit number that everyone can meet and the largest number.

Euclidean method

辗转相除法Also known as Euclidean algorithm (Euclidean algorithm), the purpose is to find out two positive integers greatest common divisor. It is the oldest known algorithm, dating back to 300 BC.

This algorithm is based on a theorem: two positive integers A and B (a greater than B), their greatest common divisor equals a divided by the remainder of B and the greatest common divisor between the smaller B.

The algorithm calculation process is this:

    • Divide 2 numbers to derive the remainder
    • If the remainder is not 0, the smaller number continues to divide with the remainder to determine whether the new remainder is 0
    • If the remainder is 0, then the greatest common divisor is the smaller number in this division.

For example, numbers 25 and 10, the greatest common divisor process is calculated using the Euclidean method as follows:

    • 25 divided by 10 quotient 2 more than 5
    • According to the Euclidean method, it can be concluded that the greatest common divisor of 25 and 10 equals the greatest common divisor between 5 and 10.
    • 10 divided by 5 quotient 2 + 0, so the greatest common divisor between 5 and 10 is 5, so 25 and 10 of greatest common divisor are 5
Topic Requirements

The function gcd of perfecting functions. The function GCD calculates and returns the largest number of conventions between the two positive integer parameters passed in

As shown below:

gcd(30,3); // 返回结果为 3gcd(12, 24); // 返回结果为 12gcd(111, 11); // 返回结果为 1

function gcd (num1,num2) {    var remainder = 0;      Do {       = num1% num2;        = num2;        = remainder;    }  while (remainder!==0);     return NUM1;} Console.log (gcd (24,12));

There are usually two ways to realize the Euclidean method, as follows

1, using the loop to achieve
functiongcd (Number1, number2) {//Create a variable that represents the remainder  varremainder = 0; //by cyclic calculation   Do {    //Update the current remainderremainder = number1%number2; //Update Number 1Number1 =number2; //Update Number 1Number2 =remainder; }  while(Remainder!== 0); returnNumber1;}
2. Using function recursion
function gcd (Number1, number2) {   if (number2 = = 0)    {return  number1;    Else  {    return gcd (number2, number1% number2);}   }

More about recursion: https://msdn.microsoft.com/zh-cn/library/wwbyhkx4.aspx

To seek greatest common divisor--[js practice by dividing the law

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.