Skip to content

Instantly share code, notes, and snippets.

@ph3nx
Last active July 17, 2021 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ph3nx/8493805 to your computer and use it in GitHub Desktop.
Save ph3nx/8493805 to your computer and use it in GitHub Desktop.
Ruby program / method that prints out all prime numbers until a given maximum.
def prime_numbers max
for i in (2..max) do
for j in (2..i) do
break if i%j == 0
end
p "#{i} is a prime number." if i == j
end
end
require 'prime'
def first_n_primes n
# Check for correct input!
"n must be an integer" unless n.is_a? Integer
"n must be greater than 0" if n <= 0
prime = Prime.instance
prime.first n
end
@steeeved
Copy link

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment