Cell division
There is a cell division cell division once every hour, once a division daughter cells after the third hour will die. So how much a child of n cells?
The core of this problem is三个小时为一个周期
// every three hours for a period of hours to go die fourth up. // A method // first hour, only a cells; the second hour, a state of cell division, the original state of a cell into the cells b, splinter cells into a new cells ; third hour, cells continue to divide into a cells and b cells of new a, b divide into cells a and c cells cells; fourth hour, a, b, c are cells split and change in accordance with the law before. The following conclusions // A one hour the initial state of an hour ago + b + c A // one hour two hours before juvenile state b A // one-hour state of three hours before the mature c b function allCell ( n-) { // A cells the let Acell = function (n-) { IF (n-==. 1 ) { return . 1 ; } the else { return Acell (. 1-n-) BCELL + (. 1-n-) CCELL + (. 1-n- ); } } // b态细胞 let bCell = function(n){ if(n==1){ return 0; }else{ return aCell(n-1); } } // c态细胞 let cCell = function(n){ if(n==1||n==2){ return 0; }else{ return bCell(n-1); } } return aCell(n)+bCell(n)+CCELL (n-) } the console.log (allCell ( 10 )) // Method II // This method is divided into living and dead function Cell (hour) { // living cells function livecell (hour) { IF (hour <4 ) { // cell death in the first three hours without power increase to 2 n-1 of the return Math.pow (2,-hour. 1 ) } the else { // the fourth hour began to cell death / / living cells = an hour before living cells - the cells dead hour return livecell (-hour. 1) * 2 - diecell (hour) } } // cell death functiondiecell (hour) { IF (hour <. 4 ) { // cell death in the first three hours without return 0 } the else { // because a three hours period // i.e. (n-3) every three hours, End cells will die // then the h (n) + dead cells in the last hours (n-1) + dead cells in the first two hours (n-2) died three hours before cells = (N- 3) cells alive return livecell (3-hour) - diecell (-hour. 1) - diecell (2-hour ) } } return livecell (hour) } the console.log (cell ( 10))
Fibonacci number
The number of columns, also known as rabbit, rabbit breeding is, for example, come to this series: 1,1,2,3,5,8 ... refers to the beginning of the third value, each value is the value of the first two with.
// recursive the let A = 0 ; function TU (NUM) { A ++ IF (|| NUM NUM. 1 == == 2 ) { return . 1 } the let the nums = TU (. 1-NUM) + TU (NUM-2 ) return the nums } the console.log (TU ( . 8 ), a) // closures solve @ is present in the array, the loop again, if the array already present, it returns the value array, the recursive function is greatly reduced number var count2 = 0 ; var FIBA = ( function () { var ARR = [0,1,1]; // bit 0 are placeholders, from a start date return function (n-) { count2 ++ ; var RES = arr [n-]; IF (RES) { // if present arr, return the value the console.log (RES, '----' ) return RES; } the else { the console.log (ARR [n-], '+++++' ) ARR [n-] = FIBA (. 1-n-) FIBA + (2-n- ); return ARR [n-]; } } }) () ; the console.log (FIBA ( . 8 ), count2) // normal // general circulation solve this problem the best performance. the let A =. 1 ; the let B =. 1 the let C; function TU (NUM) { for (the let I = 0; I <NUM-2; I ++ ) { C = A + B; A = B; B = C } return C; } the console.log (TU ( . 8))
64 grid
Grid 64, a first grid placed wheat, two second place, the third one is 4 ... Each double front grid. There are a number of tablets?
let sum = 0 let start = 1; let end = 0; function tow(){ if(end>=64){ return false } sum+=start start*=2 end++ tow() } tow() console.log(sum)
Using recursive array fast sorting method
The core idea of this is to take the middle value, and then a big aside, a small aside, and then perform the same operation on both sides, that is, recursion.
var mysort = function (ARR) { // end condition IF (arr.length <=. 1 ) { return ARR } // find the reference value of the array intermediate value // find subscript var Center = Math.floor (arr.length / 2 ) var jZ = arr.splice (Center,. 1) [0 ] // Create two empty array var left = [], right = []; // loop through the array, and compared for ( var I = 0; I < arr.length; I ++ ) { IF (ARR [I] < jZ) { left.push (ARR [I]) } the else { right.push(arr[i]) } } return mysort(left).concat([jZ],mysort(right)) } console.log(mysort([1,6,4,3,7,8,9]))
Multiplication table
// 递归 function num(nums){ if(nums==1){ console.log("1x1=1") }else{ num(nums-1) for(var i=1,str='';i<=nums;i++){ str += `${i}x${nums}=`+i*nums+' ' } console.log(str) } } num(9) // 循环 for(var i=1;i<10;i++){ let str = '' for(var j=1;j<10;j++){ if(i>=j){ str += `${j}x${i}=`+i*j+' ' } } console.log(str) }