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