Find out how many zeros there are between 1 and 1 million.

find out how many zeros there are between 1 and 1 million

Mar.07,2021

-sharp PHP
echo strlen(preg_replace('/[1-9]/', '', implode('', range(1, 1000000))));

first consider 000000 ~ 999999 (assuming you don't ignore the previous 0). There are a total of 10 ^ 6 * 6 numbers, and all 10 numbers are symmetrical. Therefore, there are a total of 10 ^ 6 * 6 code > zeros. But for the number less than 6 digits, we need to remove the 0 on the sixth bit, which has a total of 10 ^ 5, and for the number of less than 5 digits, we have to remove the 0 on the fifth bit, which has a total of 10 ^ 4.
finally add six zeros of 1 million.
so 6 * 10 ^ 5-10 ^ 5-10 ^ 4.-10-1 + 6 = 488895

Press the PHP of @ Masterton to change to JS is

var a=[], i;
for(i=0; i< 1000000; iPP) {a[i] = i+1;}
num = a.join('').replace(/[1-9]/g,'').length;

the result is also 488895

Menu