I Got a Problem like this "Count how many numbers containing number 4 among the numbers 1 to 8900 ?" what is the appropriate formula to solve the problem above, I tried to solve it by creating a looping algorithm as below
function iscontain($contain, $string){
$return = false;
$address = 0;
while($string{$address} !== ""){
if(substr($string, $address, strlen($contain)) == $contain){
$return = true;
break;
}
$address++;
}
return $return; }
$amount = 0;
for($i = 1; $i <= 10000; $i++){
if(iscontain("14", (string) $i)){
echo $i."<br />\n";
$amount++;
}
}
echo "<br />";
echo $amount;
the problem is looping algorithms have limitations, if the rate is above 10 billion, it will influence computer performance.
How many numbers between $1$ to $N$ that does not contains $4$? This can be computed in $O(\log N)$. Subtract this from $N$.