ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jan 06 2021
·
0 comments

Instructions

Test suite

Solution

Given a number n, determine what the nth prime is.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

Execute the tests with:

```
$ gradlew test
```

Use

`gradlew.bat`

if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the
`@Ignore`

annotation.

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import org.junit.Test
import org.junit.Ignore
import org.junit.Rule
import org.junit.rules.ExpectedException
import kotlin.test.assertEquals
class PrimeTest {
@Rule
@JvmField
var expectedException: ExpectedException = ExpectedException.none()
@Test
fun firstPrime() {
assertEquals(2, Prime.nth(1))
}
@Ignore
@Test
fun secondPrime() {
assertEquals(3, Prime.nth(2))
}
@Ignore
@Test
fun sixthPrime() {
assertEquals(13, Prime.nth(6))
}
@Ignore
@Test
fun bigPrime() {
assertEquals(104743, Prime.nth(10001))
}
@Ignore
@Test
fun undefinedPrime() {
expectedException.expect(IllegalArgumentException::class.java)
expectedException.expectMessage("There is no zeroth prime.")
Prime.nth(0)
}
}
```

```
object Prime {
fun nth(n: Int): Int =
when (n) {
0 -> throw IllegalArgumentException("There is no zeroth prime.")
1 -> 2 // by definition
else -> findNthOddPrime(n)
}
fun findNthOddPrime(n: Int): Int {
// We know 2 is the first and only even prime.
val primes = mutableListOf(2)
// Continue from 3 and only check odd numbers.
for (i in 3 until Int.MAX_VALUE step 2) {
// Create a sieve of the it*it primes to exhaust all possible candiates.
// We have a prime if current candiate is divisible by none of the others.
val isPrime = primes.takeWhile { it * it <= i }.all { i % it != 0 }
if (isPrime) {
primes.add(i)
if (primes.size == n) {
return i
}
}
}
throw Exception("Unexpectedly reached max integer value.")
}
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.