


Summary of common algorithms such as JS accumulation, iteration, exhaustion, and recursion
This time I will bring you a summary of the use of common algorithms such as JS accumulation, iteration, exhaustion, and recursion. What are the precautions for 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
1 2 3 4 5 6 7 8 9 |
|
Accumulation:Multiply a series of data into a variable to get the cumulative result.
The common one is the factorial of n
1 2 3 4 5 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
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
1 2 3 4 5 6 7 8 9 10 |
|
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

Recursive definition and optimization: Recursive: A function calls itself internally to solve difficult problems that can be decomposed into smaller sub-problems. Tail recursion: The function performs all calculations before making a recursive call, which can be optimized into a loop. Tail recursion optimization condition: recursive call is the last operation. The recursive call parameters are the same as the original call parameters. Practical example: Calculate factorial: The auxiliary function factorial_helper implements tail recursion optimization, eliminates the call stack, and improves efficiency. Calculate Fibonacci numbers: The tail recursive function fibonacci_helper uses optimization to efficiently calculate Fibonacci numbers.
