Skip to content

Commit 96cbd2e

Browse files
authored
Implementation of the nth-prime exercise (#422)
Implementation of the nth-prime exercise
2 parents 64fc929 + 8629700 commit 96cbd2e

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

config.json

+11
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,17 @@
520520
"matrices"
521521
]
522522
},
523+
{
524+
"slug": "nth-prime",
525+
"uuid": "8600a843-ec71-4f1a-b9fe-2ed515035101",
526+
"core": false,
527+
"unlocked_by": "hamming",
528+
"difficulty": 4,
529+
"topics": [
530+
"algorithms",
531+
"math"
532+
]
533+
},
523534
{
524535
"slug": "largest-series-product",
525536
"uuid": "b312ff0e-90e3-4dd8-99dc-df4a9deb1907",

exercises/nth-prime/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Nth Prime
2+
3+
Given a number n, determine what the nth prime is.
4+
5+
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
6+
the 6th prime is 13.
7+
8+
If your language provides methods in the standard library to deal with prime
9+
numbers, pretend they don't exist and implement them yourself.
10+
11+
## Running tests
12+
13+
In order to run the tests, issue the following command from the exercise
14+
directory:
15+
16+
For running the tests provided, `rebar3` is used as it is the official build and
17+
dependency management tool for erlang now. Please refer to [the tracks installation
18+
instructions](http://exercism.io/languages/erlang/installation) on how to do that.
19+
20+
In order to run the tests, you can issue the following command from the exercise
21+
directory.
22+
23+
```bash
24+
$ rebar3 eunit
25+
```
26+
27+
## Questions?
28+
29+
For detailed information about the Erlang track, please refer to the
30+
[help page](http://exercism.io/languages/erlang) on the Exercism site.
31+
This covers the basic information on setting up the development
32+
environment expected by the exercises.
33+
34+
## Source
35+
36+
A variation on Problem 7 at Project Euler [http://projecteuler.net/problem=7](http://projecteuler.net/problem=7)
37+
38+
## Submitting Incomplete Solutions
39+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/nth-prime/rebar.config

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
%% Erlang compiler options
2+
{erl_opts, [debug_info, warnings_as_errors]}.
3+
4+
{deps, [{erl_exercism, "0.1.2"}]}.
5+
6+
{dialyzer, [
7+
{warnings, [underspecs, no_return]},
8+
{get_warnings, true},
9+
{plt_apps, top_level_deps}, % top_level_deps | all_deps
10+
{plt_extra_apps, []},
11+
{plt_location, local}, % local | "/my/file/name"
12+
{plt_prefix, "rebar3"},
13+
{base_plt_apps, [stdlib, kernel, crypto]},
14+
{base_plt_location, global}, % global | "/my/file/name"
15+
{base_plt_prefix, "rebar3"}
16+
]}.
17+
18+
%% eunit:test(Tests)
19+
{eunit_tests, []}.
20+
%% Options for eunit:test(Tests, Opts)
21+
{eunit_opts, [verbose]}.
22+
23+
%% == xref ==
24+
25+
{xref_warnings, true}.
26+
27+
%% xref checks to run
28+
{xref_checks, [undefined_function_calls, undefined_functions,
29+
locals_not_used, exports_not_used,
30+
deprecated_function_calls, deprecated_functions]}.

exercises/nth-prime/src/example.erl

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-module(example).
2+
3+
-export([prime/1]).
4+
5+
6+
prime(N) when is_integer(N), N>0 ->
7+
Generator=prime_generator(),
8+
prime(Generator, N, []).
9+
10+
prime(Generator, N, KnownPrimes0) when length(KnownPrimes0)<N ->
11+
Next=next_candidate(Generator),
12+
KnownPrimes1=case is_prime(Next, KnownPrimes0) of
13+
true -> KnownPrimes0++[Next];
14+
false -> KnownPrimes0
15+
end,
16+
prime(Generator, N, KnownPrimes1);
17+
prime(_, _, Primes) ->
18+
lists:last(Primes).
19+
20+
is_prime(Candidate, KnownPrimes) ->
21+
SqrtCandidate=math:sqrt(Candidate),
22+
is_prime(Candidate, SqrtCandidate, KnownPrimes).
23+
24+
is_prime(_, _, []) ->
25+
true;
26+
is_prime(_, SqrtCandidate, [KnownPrime|_]) when KnownPrime>SqrtCandidate ->
27+
true;
28+
is_prime(Candidate, _, [KnownPrime|_]) when Candidate rem KnownPrime=:=0 ->
29+
false;
30+
is_prime(Candidate, SqrtCandidate, [_|KnownPrimes]) ->
31+
is_prime(Candidate, SqrtCandidate, KnownPrimes).
32+
33+
next_candidate(Generator) ->
34+
Candidate=receive {Generator, Candidate1} -> Candidate1 end,
35+
Generator ! {self(), next},
36+
Candidate.
37+
38+
prime_generator() ->
39+
Generator=spawn_link(
40+
fun () -> prime_generator_loop() end
41+
),
42+
Generator ! {self(), next},
43+
Generator.
44+
45+
prime_generator_loop() ->
46+
prime_generator_loop(2).
47+
48+
prime_generator_loop(2) ->
49+
receive {Reply, next} -> Reply ! {self(), 2} end,
50+
prime_generator_loop(3);
51+
prime_generator_loop(3) ->
52+
receive {Reply, next} -> Reply ! {self(), 3} end,
53+
prime_generator_loop(6);
54+
prime_generator_loop(N) ->
55+
receive {Reply1, next} -> Reply1 ! {self(), N-1} end,
56+
receive {Reply2, next} -> Reply2 ! {self(), N+1} end,
57+
prime_generator_loop(N+6).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{application, nth_prime,
2+
[{description, "exercism.io - nth-prime"},
3+
{vsn, "0.0.1"},
4+
{modules, []},
5+
{registered, []},
6+
{applications, [kernel,
7+
stdlib]},
8+
{env, []}
9+
]}.

exercises/nth-prime/src/nth_prime.erl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-module(nth_prime).
2+
3+
-export([prime/1]).
4+
5+
6+
prime(_N) -> undefined.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
%% Based on canonical data version 2.1.0.1
2+
%% Generated with 'testgen v0.1.0'
3+
%% https://github.com/exercism/problem-specifications/raw/4a3ba76cf247f83f2417b35df68404279942df07/exercises/nth-prime/canonical-data.json
4+
%% This file is automatically generated from the exercises canonical data.
5+
6+
-module(nth_prime_tests).
7+
8+
-include_lib("erl_exercism/include/exercism.hrl").
9+
-include_lib("eunit/include/eunit.hrl").
10+
11+
12+
13+
14+
'1_first_prime_test_'() ->
15+
{"first prime", ?_assertEqual(2, nth_prime:prime(1))}.
16+
17+
'2_second_prime_test_'() ->
18+
{"second prime", ?_assertEqual(3, nth_prime:prime(2))}.
19+
20+
'3_sixth_prime_test_'() ->
21+
{"sixth prime", ?_assertEqual(13, nth_prime:prime(6))}.
22+
23+
'4_big_prime_test_'() ->
24+
{"big prime",
25+
?_assertEqual(104743, nth_prime:prime(10001))}.
26+
27+
'5_there_is_no_zeroth_prime_test_'() ->
28+
{"there is no zeroth prime",
29+
?_assertError(_, nth_prime:prime(0))}.

0 commit comments

Comments
 (0)