Skip to content

Instantly share code, notes, and snippets.

@SolomonHD
Last active August 29, 2015 14:15
Show Gist options
  • Save SolomonHD/9b038c9e26ac769bc944 to your computer and use it in GitHub Desktop.
Save SolomonHD/9b038c9e26ac769bc944 to your computer and use it in GitHub Desktop.
require 'minitest/autorun'
require 'minitest/pride'
require 'prime'
# Write a method which returns the first n primes, where n is provided to the
# method as a parameter.
#
# Remember that the % operator (modulo) is your friend. It returns a zero if one
# number is divisible by another number. In other words, 4 % 2 == 0.
# WRITE YOUR CODE HERE. Name your method `primes`.
def primes(n)
array = []
unless n < 1
Prime.each do |prime|
array << prime
n - 1
end
end
return array
end
class PrimesChallenge < MiniTest::Test
def test_one_prime
assert_equal [2], primes(1)
end
def test_two_primes
assert_equal [2, 3], primes(2)
end
def test_ten_primes
assert_equal [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], primes(10)
end
def test_thousand_primes
first_thousand = primes(1000)
assert first_thousand.include?(6991)
assert first_thousand.include?(7907)
refute first_thousand.include?(1000)
end
def test_no_primes
assert_equal [], primes(0)
end
def test_bad_input
assert_equal [], primes(-34)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment