summaryrefslogtreecommitdiff
path: root/go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-30 04:05:06 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-30 04:05:06 +0200
commit0b2cbd28e2f47ac7d0831a139b9543cb19ba936f (patch)
tree0203bbf8c89d8cd74f6acd13fd1dbdc9c58fe442 /go
parentc43f8ced883a27f7e6cc8a90ac5c0c87bba6b052 (diff)
Solve Nth prime
Diffstat (limited to 'go')
-rw-r--r--go/nth-prime/README.md27
-rw-r--r--go/nth-prime/nth_prime.go42
-rw-r--r--go/nth-prime/nth_prime_test.go39
3 files changed, 108 insertions, 0 deletions
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)
+ }
+}