Browse Source

Merge pull request #86 from winterchord/nth_prime_exercise

Add nth-prime exercise
John Ryan 10 years ago
parent
commit
5dfd8e0ee1

+ 1
- 0
config.json View File

38
     "luhn",
38
     "luhn",
39
     "pig-latin",
39
     "pig-latin",
40
     "linked-list",
40
     "linked-list",
41
+    "nth-prime",
41
     "pascals-triangle"
42
     "pascals-triangle"
42
   ],
43
   ],
43
   "deprecated": [
44
   "deprecated": [

+ 12
- 0
nth-prime/build.gradle View File

1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.10"
11
+  testCompile "org.assertj:assertj-core:3.2.0"
12
+}

+ 0
- 0
nth-prime/src/example/java/.keep View File


+ 42
- 0
nth-prime/src/example/java/Prime.java View File

1
+import java.util.stream.IntStream;
2
+
3
+public final class Prime {
4
+    public static int nth(int nth) {
5
+        if (nth < 1) {
6
+            throw new IllegalArgumentException();
7
+        }
8
+
9
+        int primesFound = 0;
10
+        int possiblePrime = 1;
11
+
12
+        while (primesFound < nth) {
13
+            possiblePrime++;
14
+
15
+            if (isPrime(possiblePrime)) {
16
+                primesFound++;
17
+            }
18
+        }
19
+
20
+        return possiblePrime;
21
+    }
22
+
23
+    private static boolean isPrime(int n) {
24
+        if (n == 1) {
25
+            return false;
26
+        }
27
+
28
+        if (n == 2) {
29
+            return true;
30
+        }
31
+
32
+        boolean divisible = IntStream
33
+                .rangeClosed(2, (int) Math.ceil(Math.sqrt(n)))
34
+                .anyMatch((int i) -> n % i == 0);
35
+
36
+        if (divisible) {
37
+            return false;
38
+        }
39
+
40
+        return true;
41
+    }
42
+}

+ 0
- 0
nth-prime/src/main/java/.keep View File


+ 0
- 0
nth-prime/src/test/java/.keep View File


+ 30
- 0
nth-prime/src/test/java/PrimeTest.java View File

1
+import org.junit.Test;
2
+
3
+import static org.assertj.core.api.Assertions.assertThat;
4
+
5
+public class PrimeTest {
6
+    @Test
7
+    public void testFirstPrime() {
8
+        assertThat(Prime.nth(1)).isEqualTo(2);
9
+    }
10
+
11
+    @Test
12
+    public void testSecondPrime() {
13
+        assertThat(Prime.nth(2)).isEqualTo(3);
14
+    }
15
+
16
+    @Test
17
+    public void testSixthPrime() {
18
+        assertThat(Prime.nth(6)).isEqualTo(13);
19
+    }
20
+
21
+    @Test
22
+    public void testBigPrime() {
23
+        assertThat(Prime.nth(10001)).isEqualTo(104743);
24
+    }
25
+
26
+    @Test(expected = IllegalArgumentException.class)
27
+    public void testUndefinedPrime() {
28
+        Prime.nth(0);
29
+    }
30
+}

+ 1
- 0
settings.gradle View File

13
 include 'hello-world'
13
 include 'hello-world'
14
 include 'luhn'
14
 include 'luhn'
15
 include 'meetup'
15
 include 'meetup'
16
+include 'nth-prime'
16
 include 'nucleotide-count'
17
 include 'nucleotide-count'
17
 include 'octal'
18
 include 'octal'
18
 include 'pangram'
19
 include 'pangram'