A JavaScript-Based Algorithm for calculating the number of digital steps

Source: Internet
Author: User

A JavaScript-Based Algorithm for calculating the number of digital steps

These two days I have read a huge github. I know that he is interested in algorithms. I think this is a bit interesting when I read one of the algorithm for calculating numbers, so we implemented one by ourselves.

Algorithm Description and implementation principles

An integer number is given to calculate the number of steps that can reach the target. For example, a Number 4 can be used in the following steps:
Copy codeThe Code is as follows:
[1, 3]
[4]
[1, 1, 2]
[2, 2]
[1, 1, 1, 1]

In fact, the above combination can draw the following conclusion.

1. First list the combinations of all items that are 1
2. A combination of 1 from left to right
3. Recursive the set above, find the index of item 1, and then calculate the value of two items on the left. The result is recursive.
4. exclude situations 1 and 2

The following provides three tool functions:

Copy codeThe Code is as follows:
// Calculate the value in the array
Function calculate (arg ){
Return eval (arg. join ('+ '));
}

// Output the value of the array
Function print (arg ){
For (var I = 0; I <arg. length; I ++ ){
Console. log (arg [I]);
}
}

// Check whether there are positive and negative steps
Function hasRepeat (src, dist ){
If (dist. length! = 2) return false;
For (var I = 0, len = src. length; I <len; I ++ ){
If (dist. length = src [I]. length ){
If (dist [0] = src [I] [1]) {
Return true;
}
}
}
Return false;
}

The following describes the algorithm implementation:

Copy codeThe Code is as follows:
Function countSteps (n ){
Var counts = 0, I, j = 0;
Var result = [];
Var newresult = [];
Var source = [];
Var temparg = [];
// An array with all generated Items 1
For (I = 1; I <= n; I ++ ){
Source. push (1 );
}
If (n> 2 ){
For (j = 1; j <n-1; j ++ ){
Temparg. length = 0;
If (j <n-1 ){
// Generate an array with an increment of 1 from left to right
// 1. 11... 111 ..
Array. prototype. push. apply (temparg, source. slice (0, j ));
Temparg. push (calculate (source. slice (j, n )));
Result. push (temparg. slice (0 ));
// Recursive array content until there is no 1 in the item
Combine (temparg. slice (0 ));
}
}
}
// Combine array items containing 1
// 111-> 21-> 3
Function combine (arg ){
Var linearg = [];
For (var I = 0; I <arg. length; I ++ ){
If (arg [I] = 1 ){
If (I = 0 | I = 1 ){
Linearg. push (calculate (arg. slice (0, 2 )));
Array. prototype. push. apply (linearg, arg. slice (2, arg. length ));
If (! HasRepeat (result, linearg )){
Result. push (linearg );
Combine (linearg. slice (0 ));
}
Return;
}
}
}
}
// When the value is 2, one more item is required than 1.
If (n = 2 ){
Result. push ([2]);
}
// When all values are set to 1
Result. push (source );
// Output all steps
Print (result );
Console. log ('total: '+ result. length + 'walk method ');
}

// Run
CountSteps (4 );

// Output the following content
/*
[1, 3]
[4]
[1, 1, 2]
[2, 2]
[1, 1, 1, 1]
Total: 5 steps
*/

Summary

This algorithm can be applied to a certain type of game. When two objects are at a certain distance, all possible services can be processed. Of course, it can also be applied to other places, although most front-end engineers have less practice on algorithms, they still have some value. In fact, many UI details use algorithms.

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.