Pierpont primes: Difference between revisions
Content deleted Content added
Add Swift |
→{{header|Ruby}}: Add Ruby |
||
Line 2,590:
</pre>
=={{header|Ruby}}==
<lang ruby>require 'gmp'
def smooth_generator(ar)
return to_enum(__method__, ar) unless block_given?
next_smooth = 1
queues = ar.map{|num| [num, []] }
loop do
yield next_smooth
queues.each {|m, queue| queue << next_smooth * m}
next_smooth = queues.collect{|m, queue| queue.first}.min
queues.each{|m, queue| queue.shift if queue.first == next_smooth }
end
end
def pierpont(num = 1)
return to_enum(__method__, num) unless block_given?
smooth_generator([2,3]).each{|smooth| yield smooth+num if GMP::Z(smooth + num).probab_prime? > 0}
end
def puts_cols(ar, n=10)
ar.each_slice(n).map{|slice|puts slice.map{|n| n.to_s.rjust(10)}.join }
end
n, m = 50, 250
puts "First #{n} Pierpont primes of the first kind:"
puts_cols(pierpont.take(n))
puts "#{m}th Pierpont prime of the first kind: #{pierpont.take(250).last}",""
puts "First #{n} Pierpont primes of the second kind:"
puts_cols(pierpont(-1).take(n))
puts "#{m}th Pierpont prime of the second kind: #{pierpont(-1).take(250).last}"
</lang>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
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
250th Pierpont prime of the first kind: 62518864539857068333550694039553
First 50 Pierpont primes of the second kind:
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
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
</pre>
=={{header|Sidef}}==
<lang ruby>func smooth_generator(primes) {
|