From 0b2cbd28e2f47ac7d0831a139b9543cb19ba936f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 30 Aug 2016 04:05:06 +0200 Subject: Solve Nth prime --- go/nth-prime/README.md | 27 +++++++++++++++++++++++++++ go/nth-prime/nth_prime.go | 42 ++++++++++++++++++++++++++++++++++++++++++ go/nth-prime/nth_prime_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 go/nth-prime/README.md create mode 100644 go/nth-prime/nth_prime.go create mode 100644 go/nth-prime/nth_prime_test.go diff --git a/go/nth-prime/README.md b/go/nth-prime/README.md new file mode 100644 index 0000000..c485307 --- /dev/null +++ b/go/nth-prime/README.md @@ -0,0 +1,27 @@ +# Nth Prime + +Write a program that can tell you 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 simply run the command `go test` in the exercise directory. + +If the test suite contains benchmarks, you can run these with the `-bench` +flag: + + go test -bench . + +For more detailed info about the Go track see the [help +page](http://exercism.io/languages/go). + +## Source + +A variation on Problem 7 at Project Euler [http://projecteuler.net/problem=7](http://projecteuler.net/problem=7) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/go/nth-prime/nth_prime.go b/go/nth-prime/nth_prime.go new file mode 100644 index 0000000..7e01d8e --- /dev/null +++ b/go/nth-prime/nth_prime.go @@ -0,0 +1,42 @@ +package prime + +func gen() (chan int, chan bool) { + stop := make(chan bool) + ch := make(chan int) + go func() { + defer close(ch) + for i := 2; ; i++ { + select { + case ch <- i: + case <-stop: + return + } + } + }() + return ch, stop +} + +func filter(in chan int, prime int) chan int { + ch := make(chan int) + go func() { + defer close(ch) + for i := range in { + if i%prime != 0 { + ch <- i + } + } + }() + return ch +} + +func Nth(n int) (int, bool) { + if n < 1 { + return 0, false + } + ch, stop := gen() + defer func() { stop <- true }() + for i := 1; i < n; i++ { + ch = filter(ch, <-ch) + } + return <-ch, true +} diff --git a/go/nth-prime/nth_prime_test.go b/go/nth-prime/nth_prime_test.go new file mode 100644 index 0000000..03f23e8 --- /dev/null +++ b/go/nth-prime/nth_prime_test.go @@ -0,0 +1,39 @@ +package prime + +import "testing" + +var tests = []struct { + n int + p int + ok bool +}{ + {1, 2, true}, + {2, 3, true}, + {3, 5, true}, + {4, 7, true}, + {5, 11, true}, + {6, 13, true}, + {10001, 104743, true}, + {0, 0, false}, +} + +func TestNth(t *testing.T) { + for _, test := range tests { + switch p, ok := Nth(test.n); { + case !ok: + if test.ok { + t.Fatalf("Nth(%d) returned !ok. Expecting ok.", test.n) + } + case !test.ok: + t.Fatalf("Nth(%d) = %d, ok = %t. Expecting !ok.", test.n, p, ok) + case p != test.p: + t.Fatalf("Nth(%d) = %d, want %d.", test.n, p, test.p) + } + } +} + +func BenchmarkNth(b *testing.B) { + for i := 0; i < b.N; i++ { + Nth(10001) + } +} -- cgit v1.2.3