Generating Fermat primes Given a number n, print the nth prime Fermat number, where the Fermat numbers are of the form 22k+1. This code should theoretically work for any n (i.e. don't hardcode it), although it is not expected to terminate for n > 4. (It should not return 4294967297 for n=5, as 4294967297 is not a prime number.)

Do note that while all Fermat primes are of the form 22n+1, not all numbers of the form 22n+1 are prime. The goal of this challenge is to return the n-th prime.

Test cases

0 -&gt; 3
1 -&gt; 5
2 -&gt; 17
3 -&gt; 257
4 -&gt; 65537


Rules


Standard loopholes are disallowed.
0-indexing and 1-indexing are both acceptable.
This is code-golf, lowest byte-count wins.


Related: Constructible n-gons
Python 2, 53 bytes



k=input();F=2
while k:F*=F;k-=3**(F/2)%-~F/F
print-~F


Try it online!

Uses Pépin's test.



Python 2, 54 bytes



f=lambda k,F=4:k and f(k-3**(F/2)%-~F/F,F*F)or F**.5+1


Try it online!