paint-brush
Get To Know Prime: The Ruby Class For Sustaining Prime Numbersby@Kerron King
446 reads
446 reads

Get To Know Prime: The Ruby Class For Sustaining Prime Numbers

by Kerron KingFebruary 17th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Ruby makes it convenient to access prime numbers within any range with the Prime class. A prime number is a number that can only be divided by itself and one (1) The Prime class represents a set of all prime numbers. To use the prime class, ‘require’ it in your document as follows: require it in a block: reuse it to use enumerable methods. The Prime module can be used to determine the number that’s a factor of the most numbers in a list.
featured image - Get To Know Prime: The Ruby Class For Sustaining Prime Numbers
Kerron King HackerNoon profile picture

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:

require ‘prime’

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| p < 30 }

print prime 
# [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 = 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]

The “Prime.prime?” method can be used to determine whether any number passed in is prime or not.

Prime.prime?(60)

# returns false

Prime.prime?(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.

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:

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.

for i in 0...array.length
  nums << (1...array[i]).select { |x| array[i] % x == 0 }
end

3) Use for loops to determine which factors are prime numbers, using the prime array previously saved.

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

4) Count and return the most value that occurs most frequently.

mode = result.uniq.max_by{ |i| result.count(i) }

mode

Putting all this together gives us:

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

So as you can see, the Prime module as shown can be utilised in a handful of nifty ways!