I'm following the programming test, and there are questions like this
From this consecutive digits:
123456789101112131415161718192021....
For example The 10th digit is 1 The 11th digit is 0 and so on
What is the 1,000,000th digit?
What is the 1,000,000,000th digit?
What is the 1,000,000,000,000th digit?
Your solution should run below 1 second.
I've been made an answer but still more than 1 second. I try to use polynominal.
So, how I can reduce the time to find n-th place from digits above?
This is my answer, (PHP):
function digit_pol ($n) {
$counter = 1;
$digit_arr = array();
for ($i = 1; $i <= $n; $i++) {
for ($j = 0; $j < strlen($i); $j++) {
// If array more than 100, always shift (limit array length to 11)
if($i > 10) {
array_shift($digit_arr);
}
// Fill array
$digit_arr[$counter] = substr($i, $j, 1);
// Found
if($counter == $n) {
return $digit_arr[$counter];
}
$counter++;
}
}
}
/**
* TESTING
*/
$find_place = 1000000;
$result = digit_pol($find_place);
echo "Digit of " . $find_place . "th is <b>" . $result . "</b>";