Least common multiple algorithm problem harvesting

Source: Internet
Author: User
Tags gcd greatest common divisor

This problem is very typical, I took a long time to do, I wrote more than 80 lines of code to achieve the requirements of the function, speed also can be, but there are great limitations, in the 1-13 range of the results are still correct, but the larger a little, mainly in the computational method is not tenable mathematically.

In this I used a lot of functions, such as the ordering of arrays, how to determine whether a number is a prime, factorial function, Ruduce method to find the array and or product. A continuous array of known minimum and maximum values.

There are a few new gains you can focus on:

    • It is very useful to use tag to mark a Boolean value in a loop and then perform the corresponding operation by judging the value of the tag outside the loop, such as when judging prime numbers. In the same vein, count is useful for counting.
    • The conditional statement is actually quite powerful, and it's good to make the code seem very concise and elegant, such as the factorial function is typical usage (return N>1?n*fib (n-1): 1).
    • The greatest common divisor of this while statement is very classic, to understand digestion. The code is as follows:
          function gcd (A, b) {        var  temp;          while (b! = 0)            {= b            ; = a% b;             = temp;        }         return A;    }

      Can be compared with this understanding:

      // , 6 = 2, 6 2 = 0, GCD = 2

Least common multiple algorithm problem harvesting

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.