Pierpont primes: Difference between revisions
julia example
(julia example) |
|||
Line 164:
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
</pre>
=={{header|Julia}}==
The generator method is very fast but does not guarantee the primes are generated in order. Therefore we generate three times the primes needed and then sort and return the bottom third.
<lang julia>using Primes
function pierponts(N, firstkind = true)
count, ret, incdec = 0, BigInt[], firstkind ? 1 : -1
for k2 in 0:1000, k3 in 0:k2, switch in false:true
i, j = switch ? (k3, k2) : (k2, k3)
n = BigInt(2)^i * BigInt(3)^j + incdec
if !(n in ret) && isprime(n)
push!(ret, n)
if length(ret) == N * 3
return sort(ret)[1:N]
end
end
end
throw("Failed to find $N primes")
end
println("The first 50 Pierpont primes (first kind) are: ", pierponts(50))
println("\nThe first 50 Pierpont primes (second kind) are: ", pierponts(50, false))
println("\nThe 250th Pierpont prime (first kind) is: ", pierponts(250)[250])
println("\nThe 250th Pierpont prime (second kind) is: ", pierponts(250, false)[250])
</lang>{{out}}
<pre>
The first 50 Pierpont primes (first kind) are: BigInt[2, 3, 5, 7, 13, 17, 19, 37, 73, 97, 109, 163, 193, 257, 433, 487, 577, 769, 1153, 1297, 1459, 2593, 2917, 3457, 3889, 10369, 12289, 17497, 18433, 39367, 52489, 65537, 139969, 147457, 209953, 331777, 472393, 629857, 746497, 786433, 839809, 995329, 1179649, 1492993, 1769473, 1990657, 2654209, 5038849, 5308417, 8503057]
The first 50 Pierpont primes (second kind) are: BigInt[2, 3, 5, 7, 11, 17, 23, 31, 47, 53, 71, 107, 127, 191, 383, 431, 647, 863, 971, 1151, 2591, 4373, 6143, 6911, 8191, 8747, 13121, 15551, 23327, 27647, 62207, 73727, 131071, 139967, 165887, 294911, 314927, 442367, 472391, 497663, 524287, 786431, 995327, 1062881, 2519423, 10616831, 17915903, 18874367, 25509167, 30233087]
The 250th Pierpont prime (first kind) is: 62518864539857068333550694039553
The 250th Pierpont prime (second kind) is: 4111131172000956525894875083702271
</pre>
|