Definition
There is infinite row of concatenated natural numbers (positive integers, starting with 1):
1234567891011121314151617181920212223...
Challenge
- Write program in any language, that accepts position number as an input, and outputs digit from that position in the row defined above.
- Position number is arbitrary size positive integer. That is first position is 1, yielding output digit '1'
- Input is either in decimal (eg. 13498573249827349823740000191), or e-notation (eg. 1.2e789) corresponding to positive integer.
- Program has to end in reasonable time (10 seconds on modern PC/Mac), given very large index as an input (eg. 1e123456 - that is 1 with 123456 zeroes). So, simple iteration loop is not acceptable.
- Program has to terminate with an error in 1 s, if given any invalid input. Eg. 1.23e (invalid), or 1.23e1 (equals to 12.3 - not an integer)
- It's ok to use public BigNum library to parse/store numbers and do simple mathematical operations on them (+-*/ exp). No byte-penalty applied.
- Shortest code wins.
TL;DR
- Input: bignum integer
- Output: digit at that position in infinite row
123456789101112131415...
Some acceptance test cases
in notation "Input: Output". All of them should pass.
- 1: 1
- 999: 9
- 10000000: 7
- 1e7: 7 (same as row above)
- 13498573249827349823740000191: 6
- 1.1e10001: 5
- 1e23456: 5
- 1.23456e123456: 4
- 1e1000000: 0
- 1.23e: error (invalid syntax)
- 0: error (out of bounds)
- 1.23e1: error (not an integer)
Bonus!
Output digit position number inside the number, and output number itself. For example:
13498573249827349823740000191: 6 24 504062383738461516105596714
- That's digit '6' at position 24 of number '504062383738461516105596714'
1e1000000: 0 61111 1000006111141666819445...933335777790000
- Digit '0' at position 61111 of 999995-digit long number I'm not going to include here.
If you fulfill the bonus task, multiply size of your code by 0.75
Credit
This task was given at one of devclub.eu gatherings in year 2012, without large number requirement. Hence, most answers submitted were trivial loops.
1.23456e123456
arbitrarily punishes languages that cannot process such values natively and requires them to do string processing that is tangential to the challenge. \$\endgroup\$