Nθ≔¹ηW‹ⅉθ«≦⊕η≔↨E↨粬κ²ζ¿∧‹¹ζ⬤…²ζ﹪ζκ⟦Iη
Try it online! Link is to verbose version of code. Outputs the first n
Gobar primes. Explanation:
Nθ
Input n
.
≔¹η
Start at 1
(arbitrary, just has to be between 0
and 3
inclusive).
W‹ⅉθ«
Repeat until n
Gobar primes have been printed.
≦⊕η
Try the next integer.
≔↨E↨粬κ²ζ
Convert it to base 2
, flip the bits, then convert back.
¿∧‹¹ζ⬤…²ζ﹪ζκ
If the result is at least 2
and has no nontrivial proper factors, then...
⟦Iη
Print the Gobar prime on its own line.
The above algorithm appears to be O(n²) in complexity, taking 20 seconds to calculate the first 1400 Gobar primes and probably 20 minutes to calculate the first 7708. I've implemented a faster algorithm for 66 56 bytes that can calculate the first 7708 Gobar primes in 20 seconds (although it does start to slow down after that point due to using a memory-inefficient method of generating primes):
Nθ≔²η≔υζW‹Lυθ«≔Φζ‹×κκ⊗ηε≔⁺Φ⮌…η⊗η⬤ε﹪κμζζ≦⊗η≔⁺υ⁻⊖⊗ηζυ»I…υθ
Try it online! Link is to verbose version of code. Outputs the first n
Gobar primes, although it actually works by calculating the primes up to the next power of 2. Explanation:
Nθ
Input n
.
≔²η≔υζ
Start with the primes up to (but not including) 2, i.e. no primes at all.
W‹Lυθ«
Repeat until enough Gobar primes have been obtained.
≔Φζ‹×κκ⊗ηε
Get the primes up to the square root of the next power of 2.
≔⁺Φ⮌…η⊗η⬤ε﹪κμζζ
Calculate all the primes between the current and next power of 2 by trial division by all of those primes. (Sieving them would probably be even faster.)
≦⊗η
Double the current power of 2.
≔⁺υ⁻⊖⊗ηζυ
Complement the bits in all of the primes up to the current power of 2 with respect to that number of bits e.g. primes up to 4 have three bits complemented resulting in 4, 5
while primes up to 128 would have eight bits complemented. (Note that the list of primes is in reverse order so the complements are in ascending order as desired.)
»I…υθ
Output the first n
Gobar primes.