[Question]
  [
The Recursively Prime Primes is are sequence of primes such that
```
p(1) = 2
p(n) = the p(n-1)th prime
```
Here is an example of how one might calculate the 4th Recursively Prime Prime.
```
p(4) = the p(3)th prime
p(3) = the p(2)th prime
p(2) = the p(1)th prime
p(1) = 2
p(2) = the 2nd prime
p(2) = 3
p(3) = the 3rd prime
p(3) = 5
p(4) = the 5th prime
p(4) = 11
```
You should write a program or function that when given n, outputs the nth Recursively Prime Prime.
You may choose to use 0 based indexing if you wish in which case you must indicate so in your answer.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the goal is to minimize your byte count.
---
# Test Cases
```
1 -> 2
2 -> 3
3 -> 5
4 -> 11
5 -> 31
6 -> 127
7 -> 709
8 -> 5381
9 -> 52711
```
---
Relevant OEIS entry: [OEIS A007097](http://oeis.org/A007097)
  
[Answer]
# [Oasis](https://github.com/Adriandmen/Oasis), 3 bytes
The program is **0-indexed**. Code:
```
<q2
```
Uses the formula: **a(n) = nth\_prime(a(n-1) - 1)**, with the base case **a(0) = 2**.
Code explanation:
```
  2   = a(0)
<     # Decrement a(n - 1) to get a(n - 1) - 1
 q    # prime(a(n - 1) - 1)
```
[Try it online!](https://tio.run/nexus/oasis#@29TaPT//38TAA "Oasis – TIO Nexus")
[Answer]
# Mathematica, 16 bytes
```
Nest[Prime,1,#]&
```
Anonymous function. Takes a number as input and returns a number as output.
[Answer]
# [Actually](https://github.com/Mego/Seriously), 7 bytes
```
1@⌠DP⌡n
```
[Try it online!](https://tio.run/nexus/actually#@2/o8KhngUvAo56Fef//mwAA "Actually – TIO Nexus")
Explanation:
```
1@⌠DP⌡n
1        push 1
 @       swap 1 with n
  ⌠DP⌡n  do the following n times:
   DP      decrement, prime at index
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~5~~ 4 bytes
*1 byte thanks to @Dennis.*
```
1ÆN¡
```
[Try it online!](https://tio.run/nexus/jelly#@294uM3v0ML///@bAAA "Jelly – TIO Nexus")
### Explanation
```
1        Starting with n = 1,
 ÆN      replace n by the nth prime
   ¡     (input) times.
```
[Answer]
# JavaScript (ES6), 71 bytes
```
p=(n,x=1)=>n?p(n-1,(N=y=>x?N(++y,x-=(P=z=>y%--z?P(z):z==1)(y)):y)(1)):x
```
Ungolfed, you have three separate recursive functions:
```
P=(n,x=n)=>n%--x?P(n,x):x==1
N=(n,x=1)=>n?N(n-P(++x),x):x
p=(n,x=1)=>n?p(n-1,N(x)):x
```
* `P` determines whether `n` is prime;
* `N` finds the `n`th prime;
* `p` recursively runs `N` on input `1` `n` times.
[Answer]
# [MATL](https://github.com/lmendo/MATL), 6 bytes
```
1i:"Yq
```
[Try it online!](https://tio.run/nexus/matl#@2@YaaUUWfj/vyUA)
### Explanation
```
1      % Push 1
i      % Input n
:      % Range [1 2 ... N]
"      % For each (that is, do the following N times)
  Yq   %   k-th prime, where k is the input
       % End for each (implicit)
       % Display stack (implicit)
```
[Answer]
# R, ~~98~~ 93 bytes
*5 bytes thanks to @smci*
Here is a horribly inefficient recursive solution:
```
f<-function(m,n=1){j<-1;for(i in 1:n){j<-numbers::nextPrime(j)};a<-ifelse(m==0,j,f(m-1,j));a}
```
Test Output:
```
f(6)
[1] 127
f(10)        ### takes almost a minute... YIKES!!!
[1] 648391
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
ÎF<Ø
```
[Try it online!](https://tio.run/##MzBNTDJM/f//cJ@bzeEZ//9bAgA "05AB1E – Try It Online")
### Explanation
```
ÎF<Ø
Î    # Push 0 and input
 F   # Do input times...
  <   # Decrement
   Ø  # Get the nth prime (0-indexed) with n being the top of the stack
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 6 bytes
```
!¡!İp2
```
[Try it online!](https://tio.run/##yygtzv7/X/HQQsUjGwqM/v//bw4A "Husk – Try It Online")
Explanation:
```
!¡!İp2
 ¡           Make an infinite list by repeatedly applying a function
     2       Starting with 2
   İp        From the sequence of prime numbers
  !          Get the element at the given index (the previous number in the sequence)
!            Get the element at index (implicit input) in that infinite list
```
[Answer]
# Bash + common utilities, 55
Since we're doing recursive primes, here's a recursive answer:
```
((SHLVL-2<$1))&&primes 2|sed -n "`$0 $1`{p;q}"||echo 1
```
Since recursion level counting is based off the `$SHLVL` built-in variable, then the answer can be off if you're already a few shell levels deep. This is probably why this answer doesn't work on TIO.
---
If that's no good, then here's a more conventional answer:
# Bash + common utilities, 58
```
for((i=$1;i--;));{
n=`primes 2|sed -n "$n{p;q}"`
}
echo $n
```
[Try it online](https://tio.run/nexus/bash#NY3BCsIwEAXv@YpHzUEPUfQmwX@pJFuyoLs1G@mh9turIt4GBmYGrWhkLV2NwIL5uN@fl4iszqghBPi/dnLpOsQN@D7eOHHDVEjQCqHQNVO9kdlhUG0/RNL8aRrqU75pbgadBJYqj20dtG63fPHHyCHE3S7On0E/Vr6T4fQyygiCzss8xsfS9W5xlIrCy5pVaH0D).
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), ~~10~~ 8 bytes
Saved 2 bytes thanks to Adám
```
⌂pco⍣⎕⊢1
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qKO94P@jnqaC5PxHvYsf9U191LXIECT6XwEMCrgMuWAsIzjLGM4ygbNM4SwzOMsczrKAsywB "APL (Dyalog Extended) – Try It Online")
`⍣` applies the function `⌂pco` (nth prime) to 1 n times (n is taken through STDIN (`⎕`)).
## Another previous solution, 10 bytes
```
⊢∘⌂pco/⍴∘2
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/P@1R24RHXYsedcx41NNUkJyv/6h3C5BjBJQ5tOJR72ZLAA "APL (Dyalog Extended) – Try It Online")
`⍴∘2` makes a vector of n 2's, then `/` reduces using the train `⊢∘⌂pco`. `⊢` ignores its left argument, `pco` finds the nth prime, given n on the right. More generally, `⊢∘f/n⍴s` applies `f` `n-1` times to `s`.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 5 bytes
```
1$(‹ǎ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCIxJCjigLnHjiIsIiIsIjkiXQ==)
```
1$    # Push 1 and the input
  (   # Input times
   ‹ǎ # Decrement and get nth prime
```
[Answer]
# [Haskell](https://www.haskell.org/), 58 bytes
1-indexed 
```
f 1=2;f n=[x|x<-[2..],all((>)2.gcd x)[2..x-1]]!!(f(n-1)-1)
```
[Try it online!](https://tio.run/##LYyxCoMwFEX3fsUTHBJoAgpdrCl06NC5oziEmmgweUoMJYX@e6ri5cKFw@UMchmVtcm4efIBXt8lKMcf@DF@QqcwkF6Fu@8XmjQUorxqQNHEX6xZU3LenqW1hNxoyft3B5FuMLKibbOMaIKsoGuTkwYFdNMJ9sjVBzWDQ33Q2RsMuQbilewgh2Gb/VpV8MRAU7r8AQ "Haskell – Try It Online")
**Explanation:**
Uses the same 0-indexed prime-list access trick as [Adnan's answer](https://codegolf.stackexchange.com/a/110815/55329).  
Essentially straight-up follows the specification otherwise.
```
f 1=2; -- base case
f n= -- main case
    [x|x<-[2..],all((>)2.gcd x)[2..x-1]]             -- list of all primes
    [x|x<-[2..],                                     -- consider all numbers
                               [2..x-1]              -- consider all smaller numbers
                all((>)2.gcd x)                      -- is coprime with them?
                    (>)2.                            -- 2 is greater than
                         gcd x                       -- gcd(x,lambda input)
                                        !!(f(n-1)-1) -- access the
                                                     -- f(n-1)-th 1-indexed prime
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 10 bytes
```
?K2ȯ!İp₀←ε
```
[Try it online!](https://tio.run/##yygtzv7/397b6MR6xSMbCh41NTxqm3Bu6////y0A "Husk – Try It Online")
A recursive function.
[Answer]
# [Factor](https://factorcode.org/) + `math.primes.lists`, 57 bytes
```
: p ( m -- n ) [ 2 ] [ 1 - p 1 - lprimes lnth ] if-zero ;
```
[Try it online!](https://tio.run/##JY07DsIwEER7TjElFLYEHckBUJo0iAqlsKw1seQf9lLA5c1Cmle8Gc04YznXfrtO82VA8I0bouH1D12qj9T0pksl5reoxGj0fFGy1DDupnnAIwfXBxTsEaEUEg6444RFeISS4Mew7SEkOVjgnfpQzRj7GT6zkW6BloCMXfsX "Factor – Try It Online")
## Explanation
Returns the 0-indexed member of the recursively-prime prime sequence.
* `: p ( m -- n ) ... ;` Start a new word definition named `p` with stack effect `( m -- n )`, declaring it takes one thing from the data stack and leaves one thing on the data stack.
* `[ 2 ] [ ... ] if-zero` Is the input `0`? Then return `2`. Otherwise, do `...`.
* `1 - p` Apply `p` to the input minus one.
* `1 - lprimes lnth` Return the nth prime number.
[Answer]
# [Wonder](https://github.com/wonderlang/wonder), 23 bytes
```
p\.{1\2@:^(- p -#0 1)1P
```
1-indexed. Usage:
```
p\.{1\2@:^(- p -#0 1)1P}; p 3
```
# Explanation
```
p\.{                #. Pattern matching syntax
  1\2               #. Base case p(1)=2
  @:^(- p -#0 1)1P  #. Other cases p(n)=nthprime(p(n-1)-1)
                    #. nthprime is 0-indexed
}                   #. Trailing bracket is optional in this case
```
]