33
8
There are already 30 challenges dedicated to pi but not a single one asks you to find the nth decimal, so...
Challenge
For any integer in the range of 0 <= n <= 10000
display the nth decimal of pi.
Rules
- Decimals are every number after
3.
- Your program may be a function, or a full program
- You must output the result in base 10
- You may get
n
from any suitable input method (stdin, input(), function parameters, ...), but not hardcoded - You may use 1-based indexing if that's native to your language of choice
- You don't have to deal with invalid input (
n == -1
,n == 'a'
orn == 1.5
) - Builtins are allowed, if they support up to at least 10k decimals
- Runtime doesn't matter, since this is about the shortest code and not the fastest code
- This is code-golf, shortest code in bytes wins
Test cases
f(0) == 1
f(1) == 4 // for 1-indexed languages f(1) == 1
f(2) == 1 // for 1-indexed languages f(2) == 4
f(3) == 5
f(10) == 8
f(100) == 8
f(599) == 2
f(760) == 4
f(1000) == 3
f(10000) == 5
For reference, here are the first 100k digits of pi.
Built-ins? e.g.
str(pi())[n+2]
– primo – 2016-07-04T13:27:07.617What about this?
– Leaky Nun – 2016-07-04T13:27:23.937@primo Allowed. This is to find the shortest code in each language – Bassdrop Cumberwubwubwub – 2016-07-04T13:27:40.930
@primo how it is possible to generate an infinite array with codes like
pi()
and then get then+2
th digit is beyond me. – Leaky Nun – 2016-07-04T13:27:55.267@LeakyNun Related, but not a dupe imo. – Bassdrop Cumberwubwubwub – 2016-07-04T13:28:03.227
@LeakyNun that's very nearly a valid answer in Sage. – primo – 2016-07-04T13:31:58.383
6
The closest dupe targets IMO are Computing truncated digit sums powers of pi (overloads the parameter, or it would just be a finite difference applied to this challenge), Transmit pi precisely (adds an index and suppresses some printing), and Pi window encryption.
– Peter Taylor – 2016-07-04T13:38:33.750May we use base 16? – Adám – 2016-07-04T14:01:37.690
@Adám no, the result must be output in base 10 – Bassdrop Cumberwubwubwub – 2016-07-04T14:41:43.337
3@Suever ofcourse! That rule is just to point out that 10k is the minimum that your program should be able to handle – Bassdrop Cumberwubwubwub – 2016-07-04T14:59:13.230
4I suggest adding f(599) to the test cases, as it can be easy to get it wrong (you need about 3 decimals extra precision). – aditsu quit because SE is EVIL – 2016-07-04T16:04:27.337
2
Also f(760) = 4, which begins the sequence 49999998, is easy to round incorrectly.
– Anders Kaseorg – 2016-07-05T04:11:03.993@aditsu & Anders, edited them in, thanks! – Bassdrop Cumberwubwubwub – 2016-07-05T08:17:59.777
Can we use external files? – OldBunny2800 – 2016-07-22T16:37:49.783
@OldBunny2800 No, sorry. – Bassdrop Cumberwubwubwub – 2016-07-22T16:54:52.920