• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Ruby / Using Ruby to Create List of Prime Numbers

Using Ruby to Create List of Prime Numbers

October 18, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Demystifying Square Brackets: Understanding the [] Notation
  • 2.  How to Square a Number in Ruby
  • 3.  Ruby has_key Method – Check if Hash Has Key
  • 4.  How to Print Without Newline in Ruby
  • 5.  How to Print With Newline in Ruby
  • 6.  Ruby unshift Method – Add Items to Start of Array
  • 7.  Ruby Ceiling – Find the Ceiling of a Number with the ceil Method
  • 8.  Ruby Floor – Find Floor of Number with floor Method
  • 9.  Using Ruby to Capitalize First Letter of String
  • 10.  Using Ruby to Convert Array to String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy