In Ruby, we can create an array of prime numbers easily – all we need is a custom method to check if a number is prime or not. To generate an array of the first N prime numbers in Ruby, we can create our own method and loop until we have N prime numbers.

So first, here is a method to determine if a number is prime or not.

def is_prime?(num)
  2.upto(num-1) do |i|
    puts i
    if num % i == 0
      # Number is NOT prime
      return false
    end
  end
  if num > 1
    # Number IS prime
    return true
  else
    # Number is NOT prime
    return false
  end
end

Now we can use this method to help generate a list of the first N prime numbers using Ruby. We will create our own method and loop until we have N prime numbers. The method will just take one parameter, the number of prime numbers we want. And it will return an array of prime numbers. It will use the our helper method is_prime?.

def get_prime_numbers(num)
  prime_arr = []
  counter = 2;
  while prime_arr.length < num
    if is_prime?(counter)
      prime_arr.push(counter)
    end
    counter += 1
  end
  return prime_arr
end

And now, let's put these two methods together and return the first 10 prime numbers using Ruby.

def is_prime?(num)
  2.upto(num-1) do |i|
    if num % i == 0
      # Number is NOT prime
      return false
    end
  end
  if num > 1
    # Number IS prime
    return true
  else
    # Number is NOT prime
    return false
  end
end

def get_prime_numbers(num)
  prime_arr = []
  counter = 2;
  while prime_arr.length < num
    if is_prime?(counter)
      prime_arr.push(counter)
    end
    counter += 1
  end
  return prime_arr
end

print get_prime_numbers(10)

#Output:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Creating a List of Prime Numbers in a Range Using Ruby

Another example where we might want to create a list of prime numbers is if we want only the prime numbers in a specific range. If we want to create a list of prime numbers in a certain range, we can create our own method and loop over that range to find prime numbers.

We will just have to tweak our previous method, get_prime_numbers, above slightly. Below is a new method that we can use which will get the prime numbers in a range using Ruby. It will also use our helper method, is_prime?.

def get_prime_numbers_in_range(num1,num2)
  arr = []
  if num1 > num2 )
    return "Invalid Range"
  else
    if num1 < 2
      num1 = 2
    end
    num1.upto(num2) do |i|
      if is_prime?(i)
        arr.push(i)
      end
    end
    return arr
  end
end

Now lets see this in action with some examples:

def is_prime?(num)
  2.upto(num-1) do |i|
    if num % i == 0
      # Number is NOT prime
      return false
    end
  end
  if num > 1
    # Number IS prime
    return true
  else
    # Number is NOT prime
    return false
  end
end

def get_prime_numbers_in_range(num1,num2)
  arr = []
  if num1 > num2
    return "Invalid Range"
  else
    if num1 < 2
      num1 = 2
    end
    num1.upto(num2) do |i|
      if is_prime?(i)
        arr.push(i)
      end
    end
    return arr
  end
end

p get_prime_numbers_in_range(0,10)
p get_prime_numbers_in_range(-100,100)
p get_prime_numbers_in_range(3,3)
p get_prime_numbers_in_range(8,3)
p get_prime_numbers_in_range(100,200)

#Output
[2, 3, 5, 7]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[3]
"Invalid Range"
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

Hopefully this article has been useful for you to learn how to get Ruby prime numbers.

Categorized in:

Ruby,

Last Update: March 1, 2024