[Question]
  [
There are already [30 challenges dedicated to pi](https://codegolf.stackexchange.com/questions/tagged/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](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28array%29#Array%5Fsystem%5Fcross-reference%5Flist) if that's native to your language of choice
* You don't have to deal with invalid input (`n == -1`, `n == 'a'` or `n == 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](/questions/tagged/code-golf "show questions tagged '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](http://www.geom.uiuc.edu/%7Ehuberty/math5337/groupe/digits.html) are the first 100k digits of pi.
  
[Answer]
## 05AB1E, 3 bytes
```
žs¤
```
**Explained**
```
žs   # push pi to N digits
  ¤  # get last digit
```
[Try it online](http://05ab1e.tryitonline.net/#code=xb5zwqQ&input=MTAwMDE)
Uses 1-based indexing.  
Supports up to 100k digits.
[Answer]
## Python 2, 66 bytes
```
n=input()+9
x=p=5L**7
while~-p:x=p/2*x/p+10**n;p-=2
print`x/5`[-9]
```
Input is taken from stdin.
---
**Sample Usage**
```
$ echo 10 | python pi-nth.py
8
$ echo 100 | python pi-nth.py
8
$ echo 1000 | python pi-nth.py
3
$ echo 10000 | python pi-nth.py
5
```
[Answer]
# Bash + coreutils, ~~60~~ 49 bytes
~~`echo "scale=10100;4*a(1)"|bc -l|tr -d '\\\n'|cut -c$(($1+2))`~~
```
bc -l<<<"scale=$1+9;4*a(1)-3"|tr -dc 0-9|cut -c$1
```
Improved by [Dennis](https://codegolf.stackexchange.com/users/12012/dennis). Thanks!
The index is one-based.
[Answer]
# Python 2, ~~73~~ ~~71~~ 73 bytes
thanks to @aditsu for increasing my score by 2 bytes
Finally an algorithm that can complete under 2 seconds.
```
n=10**10010
a=p=2*n
i=1
while a:a=a*i/(2*i+1);p+=a;i+=1
lambda n:`p`[n+1]
```
[Ideone it!](http://ideone.com/xuf3qa)
Uses the formula `pi = 4*arctan(1)` while computing `arctan(1)` using its taylor series.
[Answer]
# MATL, ~~11~~ 10 bytes
*1 byte saved thanks to @Luis*
```
YPiEY$GH+)
```
This solution utilizes 1-based indexing
[**Try it Online**](http://matl.tryitonline.net/#code=WVBpRVkkR0grKQ&input=MTAwMDE)
[All test cases](http://matl.tryitonline.net/#code=YApZUGlFWSRAR0grKQpEVA&input=MQoyCjMKNAoxMQoxMDEKMTAwMQoxMDAwMQ)
**Explanation**
```
YP  % Pre-defined literal for pi
iE  % Grab the input and multiply by 2 (to ensure we have enough digits to work with)
Y$  % Compute the first (iE) digits of pi and return as a string
G   % Grab the input again
H+  % Add 2 (to account for '3.') in the string
)   % And get the digit at that location
    % Implicitly display the result
```
[Answer]
# Mathematica 30 bytes
```
RealDigits[Pi,10,1,-#][[1,1]]&
```
---
```
f=%
f@0
f@1
f@2
f@3
f@10
f@100
f@599
f@760
f@1000
f@10000
```
1  
4  
[Answer]
# CJam, 32
```
7e4,-2%{2+_2/@*\/2e10005+}*sq~)=
```
[Try it online](http://cjam.tryitonline.net/#code=N2U0LC0yJXsyK18yL0AqXC8yZTEwMDA1K30qc3F-KT0&input=MTAwMDA) (it's a bit slow)
[Answer]
# Sage, ~~32~~ 25 bytes
```
lambda d:`n(pi,9^5)`[d+2]
```
My first answer in a language of this kind.
`n` rounds `pi` to 17775 digits.
[Answer]
# Mathematica, ~~23~~ 21 bytes
```
⌊10^# Pi⌋~Mod~10&
```
# SageMath, 24 bytes
```
lambda n:int(10^n*pi)%10
```
[Answer]
# [J](http://jsoftware.com), ~~19~~ 15 bytes
```
10([|<.@o.@^)>:
```
Takes an integer *n* and outputs the *n**th* digit of pi. Uses zero-based indexing. To get the *n**th* digit, compute pi times 10*n*+1, take the floor of that value, and then take it modulo 10.
## Usage
The input is an extended integer.
```
   f =: 10([|<.@o.@^)>:
   (,.f"0) x: 0 1 2 3 10 100 599 760 1000
   0 1
   1 4
   2 1
   3 5
  10 8
 100 8
 599 2
 760 4
1000 3
   timex 'r =: f 10000x'
1100.73
   r
5
```
On my machine, it takes about 18 minutes to compute the 10000*th* digit.
## Explanation
```
10([|<.@o.@^)>:  Input: n
             >:  Increment n
10               The constant n
           ^     Compute 10^(n+1)
        o.@      Multiply by pi
     <.@         Floor it
   [             Get 10
    |            Take the floor modulo 10 and return
```
[Answer]
# Clojure, 312 bytes
```
(fn[n](let[b bigdec d #(.divide(b %)%2(+ n 4)BigDecimal/ROUND_HALF_UP)m #(.multiply(b %)%2)a #(.add(b %)%2)s #(.subtract % %2)](-(int(nth(str(reduce(fn[z k](a z(m(d 1(.pow(b 16)k))(s(s(s(d 4(a 1(m 8 k)))(d 2(a 4(m 8 k))))(d 1(a 5(m 8 k))))(d 1(a 6(m 8 k)))))))(bigdec 0)(map bigdec(range(inc n)))))(+ n 2)))48)))48)))
```
So, as you can probably tell, I have no idea what I'm doing. This ended up being more comical than anything. I Google'd "pi to n digits", and ended up on the [Wikipedia page for the Bailey–Borwein–Plouffe formula](https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula). Knowing just barely enough Calculus(?) to read the formula, I managed to translate it into Clojure.
The translation itself wasn't that difficult. The difficulty came from handling precision up to n-digits, since the formula requires `(Math/pow 16 precision)`; which gets huge really fast. I needed to use `BigDecimal` everywhere for this to work, which *really* bloated things up.
Ungolfed:
```
(defn nth-pi-digit [n]
  ; Create some aliases to make it more compact
  (let [b bigdec
        d #(.divide (b %) %2 (+ n 4) BigDecimal/ROUND_HALF_UP)
        m #(.multiply (b %) %2)
        a #(.add (b %) %2)
        s #(.subtract % %2)]
    (- ; Convert the character representation to a number...
      (int ; by casting it using `int` and subtracting 48
         (nth ; Grab the nth character, which is the answer
           (str ; Convert the BigDecimal to a string
             (reduce ; Sum using a reduction
               (fn [sum k]
                 (a sum ; The rest is just the formula
                       (m
                         (d 1 (.pow (b 16) k))
                         (s
                           (s
                             (s
                               (d 4 (a 1 (m 8 k)))
                               (d 2 (a 4 (m 8 k))))
                             (d 1 (a 5 (m 8 k))))
                           (d 1 (a 6 (m 8 k)))))))
               (bigdec 0)
               (map bigdec (range (inc n))))) ; Create an list of BigDecimals to act as k
           (+ n 2)))
      48)))
```
Needless to say, I'm sure there's an easier way to go about this if you know any math.
```
(for [t [0 1 2 3 10 100 599 760 1000 10000]]
  [t (nth-pi-digit t)])
([0 1] [1 4] [2 1] [3 5] [10 8] [100 8] [599 2] [760 4] [1000 3] [10000 5])
```
[Answer]
# Clojure, 253 bytes
```
(defmacro q[& a] `(with-precision ~@a))(defn h[n](nth(str(reduce +(map #(let[p(+(* n 2)1)a(q p(/ 1M(.pow 16M %)))b(q p(/ 4M(+(* 8 %)1)))c(q p(/ 2M(+(* 8 %)4)))d(q p(/ 1M(+(* 8 %)5)))e(q p(/ 1M(+(* 8 %)6)))](* a(-(-(- b c)d)e)))(range(+ n 9)))))(+ n 2)))
```
Calculate number pi using [this formula](https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula "this formula"). Have to redefine macro `with-precision` as it's used too frequently.
You can see the output here: <https://ideone.com/AzumC3>
1000 and 10000 takes exceeds time limit used on ideone, *shrugs*
[Answer]
# [Python 3](https://docs.python.org/3.6/), 338 bytes
This implementation is based on the [Chudnovsky algorithm](https://en.wikipedia.org/wiki/Chudnovsky_algorithm), one of the fastest algorithms to estimate pi. For each iteration, roughly 14 digits are estimated (take a look [here](https://www.craig-wood.com/nick/articles/pi-chudnovsky/) for further details).
```
f=lambda n,k=6,m=1,l=13591409,x=1,i=0:not i and(exec('global d;import decimal as d;d.getcontext().prec=%d'%(n+7))or str(426880*d.Decimal(10005).sqrt()/f(n//14+1,k,m,l,x,1))[n+2])or i<n and d.Decimal(((k**3-16*k)*m//i**3)*(l+545140134))/(x*-262537412640768000)+f(n,k+12,(k**3-16*k)*m
```
[Try it online!](https://repl.it/repls/DoubleStupendousBoastmachine)
[Answer]
# Smalltalk – 270 bytes
Relies on the identity `tan⁻¹(x) = x − x³/3 + x⁵/5 − x⁷/7 ...`, and that `π = 16⋅tan⁻¹(1/5) − 4⋅tan⁻¹(1/239)`. SmallTalk uses unlimited precision integer arithmetic so it will work for large inputs, if you're willing to wait!
```
|l a b c d e f g h p t|l:=stdin nextLine asInteger+1. a:=1/5. b:=1/239. c:=a. d:=b. e:=a. f:=b. g:=3. h:=-1. l timesRepeat:[c:=c*a*a. d:=d*b*b. e:=h*c/g+e. f:=h*d/g+f. g:=g+2. h:=0-h]. p:=4*e-f*4. l timesRepeat:[t:=p floor. p:=(p-t)*10]. Transcript show:t printString;cr
```
Save as `pi.st` and run as in the following test cases. Indexing is one based.
```
$ gst -q pi.st <<< 1
1
$ gst -q pi.st <<< 2
4
$ gst -q pi.st <<< 3
1
$ gst -q pi.st <<< 4
5
$ gst -q pi.st <<< 11
8
$ gst -q pi.st <<< 101
8
$ gst -q pi.st <<< 600
2
$ gst -q pi.st <<< 761
4
$ gst -q pi.st <<< 1001
3
$ gst -q pi.st <<< 10001 -- wait a long time!
5
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org) (Chrome 67+), ~~75~~ ~~73~~ ~~67~~ 63 bytes
```
n=>`${eval(`for(a=c=100n**++n*20n,d=1n;a*=d;)c+=a/=d+++d`)}`[n]
```
[Try it online!](https://tio.run/##pU7LisIwFN37FUHKcNNk0lTRoZTrYnZ@gzg0JKlU4k1pRYRhvr3Gx8L9nMXhPBbnHM3FjHbo@vMnReenFifCTZP9@osJ0LRxAIMWS60pz4WgfKFJOiypNjm6mluBpkAnhHAN/2t2tJ/qnZaslGwh2TKJu9GJVlUl2df6aV@s9@pkergy3DAbaYzBqxAP0MJ3d9jSGa48oZ7N3kv4z0EOj13iagyd9fA6UnI1@D6YlBQ/oHgh2Tz7UPM0Pt0A "JavaScript (Node.js) – Try It Online")
Using \$\pi/2=\sum\_{k=0}^{\infty}k!/(2k+1)!!\$ (same logic used by Leaky Nun's Python answer, but thanks to the syntax of JS that makes this shorter). Input is passed to the function as a BigInt. 2 bytes can be removed if 1-based indexing is used:
```
n=>`${eval(`for(a=c=100n**n*20n,d=1n;a*=d;)c+=a/=d+++d`)}`[n]
```
# [JavaScript (Node.js)](https://nodejs.org) (Chrome 67+), ~~90~~ 89 bytes
```
n=>`${eval(`for(a=100n**++n*2n,b=a-a/3n,c=0n,d=1n;w=a+b;a/=-4n,b/=-9n,d+=2n)c+=w/d`)}`[n]
```
[Try it online!](https://tio.run/##tY5BTsMwEEX3PYVVVWgcO7aTQlEUTRfsOENVFNdxqlRmHCVVWwlx9mCgCy7ALP7Mn7d5J3uxkxv74ZxTbP3c4Uy4bVYf/mIDNF0cwWJhDGWZEJSVJA9oc6vXJB0aki0WVF/RikNtNeaPiadVJSCwJO4EXnXb8M9mR/u53hnJCslKydbp@C4mxVNVSfa8@a33NHv1bge4MdwyF2mKwasQj9DBS398pTPceJp6sfgL4X/UOfwYEVdT6J2Hu2LB1eiHYNNHv4HiWrLl6kEtk9b8BQ "JavaScript (Node.js) – Try It Online")
Using \$\pi/4=\arctan(1/2)+\arctan(1/3)\$. Input is passed to the function as a BigInt. 2 bytes can be removed if 1-based indexing is used:
```
n=>`${eval(`for(a=100n**n*2n,b=a-a/3n,c=0n,d=1n;w=a+b;a/=-4n,b/=-9n,d+=2n)c+=w/d`)}`[n]
```
[Answer]
# Java 7, ~~262~~ 260 bytes
```
import java.math.*;int c(int n){BigInteger p,a=p=BigInteger.TEN.pow(10010).multiply(new BigInteger("2"));for(int i=1;a.compareTo(BigInteger.ZERO)>0;p=p.add(a))a=a.multiply(new BigInteger(i+"")).divide(new BigInteger((2*i+++1)+""));return(p+"").charAt(n+1)-48;}
```
Used [*@LeakyNun's* Python 2 algorithm](https://codegolf.stackexchange.com/a/84465/52210).
**Ungolfed & test code:**
[Try it here.](https://ideone.com/eqlwsu)
```
import java.math.*;
class M{
  static int c(int n){
    BigInteger p, a = p = BigInteger.TEN.pow(10010).multiply(new BigInteger("2"));
    for(int i = 1; a.compareTo(BigInteger.ZERO) > 0; p = p.add(a)){
      a = a.multiply(new BigInteger(i+"")).divide(new BigInteger((2 * i++ + 1)+""));
    }
    return (p+"").charAt(n+1) - 48;
  }
  public static void main(String[] a){
    System.out.print(c(0)+", ");
    System.out.print(c(1)+", ");
    System.out.print(c(2)+", ");
    System.out.print(c(3)+", ");
    System.out.print(c(10)+", ");
    System.out.print(c(100)+", ");
    System.out.print(c(599)+", ");
    System.out.print(c(760)+", ");
    System.out.print(c(1000)+", ");
    System.out.print(c(10000));
  }
}
```
**Output:**
```
1, 4, 1, 5, 8, 8, 2, 4, 3, 5
```
[Answer]
# Maple, 24 bytes
```
 trunc(10^(n+1)*Pi)mod 10
```
Test cases:
```
> f:=n->trunc(10^(n+1)*Pi)mod 10;
> f(0);
  1
> f(1);
  4
> f(2);
  1
> f(3);
  5
> f(10);
  8
> f(100);
  8
> f(599);
  2
> f(760);
  4
> f(1000);
  3
> f(10000);
  5
```
]