This time I will bring you a summary of the use of common algorithms such as JS accumulation, iteration, exhaustion, and recursion. What are theprecautionsfor the use of common algorithms such as JS accumulation, iteration, exhaustion, and recursion, as follows. This is a practical case, let’s take a look at it.
Accumulation and accumulation
Accumulation:Add a series of data to a variable. The final cumulative result is obtained
For example: Calculate the cumulative sum of the numbers from 1 to 100
The ball falls from a height and returns to half of the original value each time. Find the tenth time the ball lands. The distance traveled by the ball in time
Accumulation:Multiply a series of data into a variable to get the cumulative result.
The common one is the factorial of n
var n=100; var result= 1; for(var i=1;i<=n;i++){ result *=i; }
General form:
Accumulation: V =e;
Accumulation: v*=e;
V represents accumulation and accumulation, e represents accumulation/accumulation term
Key points of the algorithm:
(1) Initialization
Initialize v and e
Accumulation: v = 0;
Accumulation: v = 1;
e initialization, if the accumulation/product item is complex, it may be decomposed into several sub-items and initialized separately , such as the problem of calculating pi, the cumulative term is decomposed into three parts: symbol, numerator and denominator.
(2) Loop control conditions
One is a fixed number of times, such as the problem of calculating the bounce distance, the problem of calculating the sum of the first 20 items of the sequence,
number of times It is not fixed, but must meet a certain condition: the problem of calculating pi requires that the absolute value of the last term be less than 10-6.
(3) Determine the change of the accumulation/product term
For example, the sum of the first 20 terms of a sequence is to use the sum of the current numerator and denominator as the next denominator, and the current denominator as the numerator .
Another example is the problem of finding pi, which is to invert the sign, add 2 to the denominator, and then find the next term.
Iteration
The iteration method is also the rolling method
Rule: it can be used continuously The old value gets the new value until we get the result we want.
How to solve the problem of iteration
1. Find the iteration variable (old value)
2. Determine the relationship between iteration
3. Knowing what the desired result is (conditions for ending the loop)
(1) is to know the final result
(2) The number of loops
Recursion
Find the mathematical rule: calculate the value of the next item through the formula until the result we want
For example: a rabbit gives birth: through the first two The item gets the next item
Recursion is divided into forward push and reverse push.
Exhaustion
When you encounter a problem and can’t find a better solution (can’t find a mathematical formula or rule) , use the "stupidest" method, take advantage of the fast computing speed of computers, list all possibilities
and record the results we want
Exhaustive method Its characteristics are: the algorithm is simple and the corresponding program is also simple, but the amount of calculation is often large. However, the advantage of computers is their fast computing speed, so this algorithm can maximize its strengths and avoid weaknesses, and can often achieve good results.
Case: There is a three-digit number, the ones digit is larger than the hundreds digit, and the hundreds digit is larger than the tens digit, and the sum of the digits is equal to the product of the multiplication of the digits, find these three Number of digits
Recursion
The so-called recursion is to call yourself again inside the function.
For example, to find the factorial problem, the fact function is called inside the fact function
The recursive algorithm is very complicated to understand according to the conventional thinking, and the function calls are nested layer by layer. Call, and then return layer by layer, you might as well change your mind to understand recursion.
Recursion is actually solving a problem of size n by reducing it to a problem of n-1. That is to find the relationship between n and n-1.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Summary of common JS algorithm examples
##Detailed explanation of JavaScript callback function use cases
The above is the detailed content of Summary of common algorithms such as JS accumulation, iteration, exhaustion, and recursion. For more information, please follow other related articles on the PHP Chinese website!