Last active
July 17, 2021 07:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool