| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Japt h | 250509T112823Z | Shaggy |
| 029 | Juby | 240302T183813Z | Jordan |
| 034 | PARI/GP | 240229T122858Z | 138 Aspe |
| 085 | Python 3 | 240227T161739Z | Mukundan |
| 057 | Factor + math.primes.lists | 220109T020428Z | chunes |
| 005 | Vyxal | 220109T012052Z | emanresu |
| 008 | APL Dyalog Extended | 201223T202304Z | user |
| 006 | Husk | 201108T172052Z | user |
| 010 | Husk | 201010T144938Z | Razetime |
| 004 | 05AB1E | 171007T192033Z | Datboi |
| 058 | Haskell | 171007T171020Z | SEJPM |
| 023 | Wonder | 170222T042434Z | Mama Fun |
| 093 | R | 170221T204901Z | Joseph W |
| 055 | Bash + common utilities | 170221T210538Z | Digital |
| 006 | MATL | 170221T183816Z | Luis Men |
| 071 | JavaScript ES6 | 170221T191354Z | ETHprodu |
| 004 | Jelly | 170221T184539Z | PurkkaKo |
| 003 | Oasis | 170221T182449Z | Adnan |
| 016 | Mathematica | 170221T182326Z | LegionMa |
| 007 | Actually | 170221T182155Z | user4594 |
Japt -h, 10 bytes
Æ=Èj}iX©UÉ
Æ=Èj}iX©UÉ :Implicit input of integer U
Æ :Map each X in the range [0,U)
= : Reassign to U
È : Function taking an integer as argument
j : Is prime?
} : End function
i : Get Nth integer that returns true, where N is
X© : Logical AND of X and
UÉ : U-1
J-uby, 29 bytes
:**&(:take&Prime|:last)|~:^&1
Explanation
:take & Prime | :last # nth prime
:** & ( ) | ~:^ & 1 # ...applied to 1 input times
Python 3, 85 bytes
p=lambda x:[i for i in range(2,5*x**x)if all(i%j for j in range(2,i))][x and~-p(x-1)]
Uses 0-indexing.
Factor + math.primes.lists, 57 bytes
: p ( m -- n ) [ 2 ] [ 1 - p 1 - lprimes lnth ] if-zero ;
Explanation
Returns the 0-indexed member of the recursively-prime prime sequence.
: p ( m -- n ) ... ;Start a new word definition namedpwith stack effect( m -- n ), declaring it takes one thing from the data stack and leaves one thing on the data stack.[ 2 ] [ ... ] if-zeroIs the input0? Then return2. Otherwise, do....1 - pApplypto the input minus one.1 - lprimes lnthReturn the nth prime number.
APL (Dyalog Extended), 10 8 bytes
Saved 2 bytes thanks to Adám
⌂pco⍣⎕⊢1
⍣ applies the function ⌂pco (nth prime) to 1 n times (n is taken through STDIN (⎕)).
Another previous solution, 10 bytes
⊢∘⌂pco/⍴∘2
⍴∘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.
Husk, 6 bytes
!¡!İp2
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
05AB1E, 4 bytes
ÎF<Ø
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
Haskell, 58 bytes
1-indexed
f 1=2;f n=[x|x<-[2..],all((>)2.gcd x)[2..x-1]]!!(f(n-1)-1)
Explanation:
Uses the same 0-indexed prime-list access trick as Adnan's answer.
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
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
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
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
MATL, 6 bytes
1i:"Yq
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)
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
Pdetermines whethernis prime;Nfinds thenth prime;precursively runsNon input1ntimes.
Jelly, 5 4 bytes
1 byte thanks to @Dennis.
1ÆN¡
Explanation
1 Starting with n = 1,
ÆN replace n by the nth prime
¡ (input) times.
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)
Mathematica, 16 bytes
Nest[Prime,1,#]&
Anonymous function. Takes a number as input and returns a number as output.
Actually, 7 bytes
1@⌠DP⌡n
Explanation:
1@⌠DP⌡n
1 push 1
@ swap 1 with n
⌠DP⌡n do the following n times:
DP decrement, prime at index