RosettaCodeData/Task/Anti-primes/Ruby/anti-primes.rb

19 lines
461 B
Ruby
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
require 'prime'
def num_divisors(n)
2023-07-18 13:51:12 -07:00
n.prime_division.inject(1){|prod, (_p,n)| prod * (n + 1) }
2023-07-01 11:58:00 -04:00
end
anti_primes = Enumerator.new do |y| # y is the yielder
max = 0
y << 1 # yield 1
2.step(nil,2) do |candidate| # nil is taken as Infinity
num = num_divisors(candidate)
if num > max
y << candidate # yield the candidate
max = num
end
end
end
puts anti_primes.take(20).join(" ")