visit
require ‘prime’
prime = Prime.take_while { |p| p < 30 }
print prime
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
num = 50
prime_nums = Prime::EratosthenesGenerator.new.take_while
{ |prime| prime <= num }
print prime_nums
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Prime.prime?(60)
# returns false
Prime.prime?(5)
# returns true
require "prime"
def number_of_primes(array)
prime_arr = Prime.take_while { |p| p < 500 }
num = array.count do |x|
prime_arr.include?(x)
end
num
end
puts number_of_primes([121, 17, 21, 29, 11, 341, 407, 19, 119, 352])
# 4
Lastly, the Prime module can be used to determine the number that’s a factor of the most numbers in a given list. To achieve this, the following steps can be taken:
prime_arr = Prime.take_while { |x| x < 10_000 }
for i in 0...array.length
nums << (1...array[i]).select { |x| array[i] % x == 0 }
end
for i in 0...nums.length
for j in 0...nums.length
if prime_arr.include?(nums[i][j])
result << nums[i][j]
end
end
end
mode = result.uniq.max_by{ |i| result.count(i) }
mode
require 'prime'
def prime_prime(array)
nums = []
result = []
prime_arr = Prime.take_while { |x| x < 10_000 }
for i in 0...array.length
nums << (1...array[i]).select { |x| array[i] % x == 0 }
end
for i in 0...nums.length
for j in 0...nums.length
if prime_arr.include?(nums[i][j])
result << nums[i][j]
end
end
end
mode = result.uniq.max_by{ |i| result.count(i) }
mode
end
puts prime_prime([2, 3, 5, 6, 9])
# => 3
puts prime_prime([121, 17, 21, 29, 11, 341, 407, 19, 119, 352])
# => 11