# Calculate the number of occurrences of 1 in 10 billion (not limited to 10 billion and 1)

Source: Internet
Author: User

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`

Related Keywords:
Related Article

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.