I used to take a written test. One of the questions in the test was to calculate the number of times that 1 appeared within 40 billion. At that time, I only gave the idea. The specific implementation was not written. Now I will list the specific implementation code. The following code can only calculate the number less than the N power of 10, instead of the x * 10 ^ N power. To expand the calculation, we also need to... SyntaxHighlighter. all ();
I used to take a written test. One of the questions in the test was to calculate the number of times that 1 appeared within 40 billion. At that time, I only gave the idea. The specific implementation was not written. Now I will list the specific implementation code.
The following code can only calculate the number less than the N power of 10, rather than the x 10 ^ N power. To expand the computation, you must complete the code.
* Calculate the number of times 1 appears within the Npower of 10 */function calc (n) {var number = n. toString (); // convert a number to a string // console. log (number); var len = number. length-1; // does not contain, So-1 // console. log (len); // var first = Number (nunber. slice (); // extended, used to calculate if (len> 1) Within x * 10 ^ n {// recursive calculation console if it is more than 2 digits. log (fac (len);} else {// if it is 1-digit, there is only 1 console. log (1) ;}/// recursive function. m indicates the length of the number to be calculated. 100 indicates m = 2 function fac (m) {if (m> 1) {return 10 * fac (S-1) + Math. pow (10 m-1);} else {return 1 ;}} calc (10); // => 1 calc (100); // => 20 calc (1000 ); // => 300 calc (10000); // => 4000 calc (Math. pow (1 billion); // 6513215599 =>/** calculate the number of times 1 appears within the Npower of 10 */function calc (n) {var number = n. toString (); // convert a number to a string // console. log (number); var len = number. length-1; // does not contain, So-1 // console. log (len); // var first = Number (nunber. slice (); // extended, used to calculate if (len> 1) Within x * 10 ^ n {// recursive calculation console if it is more than 2 digits. log (fac (len);} else {// if it is 1-digit, there is only 1 console. log (1) ;}/// recursive function. m indicates the length of the number to be calculated. 100 indicates m = 2 function fac (m) {if (m> 1) {return 10 * fac (S-1) + Math. pow (10 m-1);} else {return 1 ;}} calc (10); // => 1 calc (100); // => 20 calc (1000 ); // => 300 calc (10000); // => 4000 calc (Math. pow (1 billion); // 6513215599 =>
The modified program can calculate the number of m in the Npower power of x multiplied by 10.
** Calculate the number of times 1 appears within the Npower of x multiplied by 10 */function calc (n) {var number = n. toString (); // convert a Number to a string var first = number (Number. slice (); // extended, used to calculate var len = (first> 1? Number. length :( number. length-1); if (len> 1) {// if it is more than 2 digits, recursive calculation console. log (fac (len, first);} else {// if it is 1-digit, there is only 1 console. log (1) ;}/// recursive function. m indicates the length of the number to be calculated. 100 indicates m = 2. k indicates the first function of the number. fac (m, k) {if (m> 1) {if (k = 1) {return 10 * fac (S-1, 1) + Math. pow (10 m-1);} else {return k * fac (S-1, 1) + Math. pow (10 m-1) ;}} else {return 1 ;}} calc (10); // => 1 calc (20); calc (30 ); calc (40); calc (100); // => 20 calc (200); // => 140/** calculate x by 1 Number of times 1 appears within the power of N0 */function calc (n) {var number = n. toString (); // convert a Number to a string var first = number (Number. slice (); // extended, used to calculate var len = (first> 1? Number. length :( number. length-1); if (len> 1) {// if it is more than 2 digits, recursive calculation console. log (fac (len, first);} else {// if it is 1-digit, there is only 1 console. log (1) ;}/// recursive function. m indicates the length of the number to be calculated. 100 indicates m = 2. k indicates the first function of the number. fac (m, k) {if (m> 1) {if (k = 1) {return 10 * fac (S-1, 1) + Math. pow (10 m-1);} else {return k * fac (S-1, 1) + Math. pow (10 m-1) ;}} else {return 1 ;}} calc (10); // => 1 calc (20); calc (30 ); calc (40); calc (100); // => 20 calc (200); // => 140