A prime number is a number that can only be divided by itself and one (1). Ruby makes it convenient to access prime numbers within any range with the Prime class. This class represents a set of all prime numbers. Ruby also offers several methods for accessing the values herein. To use the prime class, ‘require’ it in your document as follows: ‘prime’ require To return an array of all prime numbers within a certain range, the Prime class can be used with enumerable methods. Simply pass in a block as follows: prime = Prime.take_while { p < } print prime |p| 30 # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] This can also be done by utilising the prime Eratosthenes generator. This uses the Eratosthenes method under the hood to generate prime numbers as needed: num = prime_nums = Prime::EratosthenesGenerator.new.take_while { prime <= num } print prime_nums 50 |prime| # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] The “Prime.prime?” method can be used to determine whether any number passed in is prime or not. Prime.prime?( ) Prime.prime?( ) 60 # returns false 5 # returns true These simple, yet powerful methods can be used along with other enumerable Ruby methods to solve other problems. Determining the number of prime numbers that exist within a specified range of numbers is one such possibility. prime_arr = Prime.take_while { p < } num = array.count prime_arr. ?(x) num puts number_of_primes([ , , , , , , , , , ]) require "prime" def number_of_primes (array) |p| 500 do |x| include end end 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: 1) Retrieve an array of prime numbers before the highest number in the set: prime_arr = Prime.take_while { x < } |x| 10_000 2) Determine the factors of each number in the list, storing those values in a holder. i ...array.length nums << ( ...array[i]).select { array[i] % x == } for in 0 1 |x| 0 end 3) Use for loops to determine which factors are prime numbers, using the prime array previously saved. i ...nums.length j ...nums.length prime_arr. ?(nums[i][j]) result << nums[i][j] for in 0 for in 0 if include end end end 4) Count and return the most value that occurs most frequently. mode = result.uniq.max_by{ result.count(i) } mode |i| Putting all this together gives us: nums = [] result = [] prime_arr = Prime.take_while { x < } i ...array.length nums << ( ...array[i]).select { array[i] % x == } i ...nums.length j ...nums.length prime_arr. ?(nums[i][j]) result << nums[i][j] mode = result.uniq.max_by{ result.count(i) } mode puts prime_prime([ , , , , ]) puts prime_prime([ , , , , , , , , , ]) require 'prime' def prime_prime (array) |x| 10_000 for in 0 1 |x| 0 end for in 0 for in 0 if include end end end |i| end 2 3 5 6 9 # => 3 121 17 21 29 11 341 407 19 119 352 # => 11 So as you can see, the Prime module as shown can be utilised in a handful of nifty ways!