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.

To run the tests run the command `go test`

from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench`

and `--benchmem`

flags:

```
go test -v --bench . --benchmem
```

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

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.

```
package prime
// Source: exercism/problem-specifications
// Commit: 4a3ba76 nth-prime: Apply new "input" policy
// Problem Specifications Version: 2.1.0
var tests = []struct {
description string
n int
p int
ok bool
}{
{
"first prime",
1,
2,
true,
},
{
"second prime",
2,
3,
true,
},
{
"sixth prime",
6,
13,
true,
},
{
"big prime",
10001,
104743,
true,
},
{
"there is no zeroth prime",
0,
0,
false,
},
}
```

```
package prime
import "testing"
func TestNth(t *testing.T) {
for _, test := range tests {
switch p, ok := Nth(test.n); {
case !ok:
if test.ok {
t.Fatalf("FAIL %s\nNth(%d) returned !ok. Expecting ok.", test.description, test.n)
}
case !test.ok:
t.Fatalf("FAIL %s\nNth(%d) = %d, ok = %t. Expecting !ok.", test.description, test.n, p, ok)
case p != test.p:
t.Fatalf("FAIL %s\nNth(%d) = %d, want %d.", test.description, test.n, p, test.p)
}
t.Logf("PASS %s", test.description)
}
}
func BenchmarkNth(b *testing.B) {
for i := 0; i < b.N; i++ {
Nth(10001)
}
}
```

```
package prime
// Nth returns the nth prime.
func Nth(n int) (int, bool) {
if n < 1 {
return 0, false
}
primes := make([]int, n)
primes[0] = 2
for i := 1; i < n; i++ {
candidate := primes[i-1] + 1
for !IsPrime(candidate, primes) {
candidate++
}
primes[i] = candidate
}
return primes[len(primes)-1], true
}
// IsPrime determines whether the candidate is divisible by a list of prime numbers.
func IsPrime(candidate int, primes []int) bool {
for _, prime := range primes {
if prime > 1 && candidate%prime == 0 {
return false
}
}
return true
}
```

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,081 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments