Hilbert Primes Golf
Haskell, 46 bytes
(foldr(\a b->a:[x|x<-b,mod x a>0])[][5,9..]!!)
An anonymous function.
The core is foldr(\a b->a:[x|x<-b,mod x a>0])[][5,9..]
, which iterates through the arithmetic progression 5,9,13,...
, removing multiples of each one from the list to its right. This produces the infinite list of Hilbert primes. Then, !!
takes the n
th element.
I has tried making (\a b->a:[x|x<-b,mod x a>0])
pointfree but didn't find a shorter way.
CJam, 36 33 32 23 bytes
5ri{_L+:L;{4+_Lf%0&}g}*
Try it online
The latest version is actually much more @MartinBüttner's than mine. The key idea in his suggested solution is to use two nested loops to find the n-th value that meets the condition. I thought I was being clever by using only a single loop in my original solution, but it turns out that the added logic cost more than I saved by not using a second loop.
Explanation
5 Push first Hilbert prime.
ri Get input n and convert to integer.
{ Loop n times.
_ Push a copy of current Hilbert prime.
L Push list of Hilbert primes found so far (L defaults to empty list).
+ Prepend current Hilbert prime to list.
:L Store new list of Hilbert primes in variable L.
; Pop list off stack.
{ Start while loop for finding next Hilbert prime.
4+ Add 4 to get next Hilbert number.
_ Copy candidate Hilbert number.
L Push list of Hilbert primes found so far.
f% Element wise modulo of Hilbert number with smaller Hilbert primes.
0& Check for 0 in list of modulo values.
}g End while loop.
}* End loop n times.
Pyth, 21 bytes
Lh*4bye.fqZf!%yZyT1hQ
Try it online: Demonstration or Test Suite
Explanation:
Lh*4bye.fqZf!%yZyT1Q implicit: Q = input number
L define a function y(b), which returns
h*4b 4*b + 1
this converts a index to its Hilbert number
.f hQ find the first (Q+1) numbers Z >= 1, which satisfy:
f 1 find the first number T >= 1, which satisfies:
!%yZyT y(Z) mod y(T) == 0
qZ test if the result is equal to Z
this gives a list of indices of the first Q Hilbert Primes
e take the last index
y apply y and print