Skip to content

Add zeroth prime test case. Fixes #231 #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion exercises/nth-prime/example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ object Prime {
private lazy val primes: Stream[BigInt] =
Stream.cons(BigInt(2), primes.map(b => new BigInt(b.bigInteger.nextProbablePrime)))

def nth(n: Int): Int = primes.drop(n - 1).head.toInt
def nth(n: Int): Option[Int] =
if (n < 1) None
else Some(primes.drop(n - 1).head.toInt)
}
17 changes: 11 additions & 6 deletions exercises/nth-prime/src/test/scala/PrimeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@ import org.scalatest.{Matchers, FlatSpec}

class PrimeTest extends FlatSpec with Matchers {
it should "calculate 1st prime" in {
Prime.nth(1) should be (2)
Prime.nth(1) should be (Some(2))
}

it should "calculate 2nd prime" in {
pending
Prime.nth(2) should be (3)
Prime.nth(2) should be (Some(3))
}

it should "calculate 6th prime" in {
pending
Prime.nth(6) should be (13)
Prime.nth(6) should be (Some(13))
}

it should "calculate 1000th prime" in {
pending
Prime.nth(1000) should be (7919)
Prime.nth(1000) should be (Some(7919))
}

it should "calculate 10000th prime" in {
pending
Prime.nth(10000) should be (104729)
Prime.nth(10000) should be (Some(104729))
}

it should "calculate 10001th prime" in {
pending
Prime.nth(10001) should be (104743)
Prime.nth(10001) should be (Some(104743))
}

it should "not find zeroth prime" in {
pending
Prime.nth(0) should be (None)
}
}