aboutsummaryrefslogtreecommitdiff
path: root/nth-prime
diff options
context:
space:
mode:
authorFederico I <git@federicoigne.com>2020-03-16 14:42:35 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:54:35 +0000
commita4b6e4b2e898b7efa0b44b490bd731557b0cc41b (patch)
tree2eb81879f22eb42e8884fa3fbea13be396866b75 /nth-prime
parent6d468ece96c1e89f73828369be907d17513b455f (diff)
downloadexercism-a4b6e4b2e898b7efa0b44b490bd731557b0cc41b.tar.gz
exercism-a4b6e4b2e898b7efa0b44b490bd731557b0cc41b.zip
[rust] Nth Prime
Diffstat (limited to 'nth-prime')
-rw-r--r--nth-prime/.exercism/metadata.json1
-rw-r--r--nth-prime/.gitignore8
-rw-r--r--nth-prime/Cargo.toml6
-rw-r--r--nth-prime/README.md92
-rw-r--r--nth-prime/src/lib.rs8
-rw-r--r--nth-prime/tests/nth-prime.rs21
6 files changed, 136 insertions, 0 deletions
diff --git a/nth-prime/.exercism/metadata.json b/nth-prime/.exercism/metadata.json
new file mode 100644
index 0000000..5c5337e
--- /dev/null
+++ b/nth-prime/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"nth-prime","id":"7ed3faca556f4c23a55b30fae8b69047","url":"https://exercism.io/my/solutions/7ed3faca556f4c23a55b30fae8b69047","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/nth-prime/.gitignore b/nth-prime/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/nth-prime/.gitignore
@@ -0,0 +1,8 @@
1# Generated by Cargo
2# will have compiled files and executables
3/target/
4**/*.rs.bk
5
6# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8Cargo.lock
diff --git a/nth-prime/Cargo.toml b/nth-prime/Cargo.toml
new file mode 100644
index 0000000..fc1c776
--- /dev/null
+++ b/nth-prime/Cargo.toml
@@ -0,0 +1,6 @@
1[package]
2edition = "2018"
3name = "nth_prime"
4version = "2.1.0"
5
6[dependencies]
diff --git a/nth-prime/README.md b/nth-prime/README.md
new file mode 100644
index 0000000..425abde
--- /dev/null
+++ b/nth-prime/README.md
@@ -0,0 +1,92 @@
1# Nth Prime
2
3Given a number n, determine what the nth prime is.
4
5By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
6the 6th prime is 13.
7
8If your language provides methods in the standard library to deal with prime
9numbers, pretend they don't exist and implement them yourself.
10
11Remember that while people commonly count with 1-based indexing (i.e. "the 6th prime is 13"), many programming languages, including Rust, use 0-based indexing (i.e. `primes[5] == 13`). Use 0-based indexing for your implementation.
12
13
14## Rust Installation
15
16Refer to the [exercism help page][help-page] for Rust installation and learning
17resources.
18
19## Writing the Code
20
21Execute the tests with:
22
23```bash
24$ cargo test
25```
26
27All but the first test have been ignored. After you get the first test to
28pass, open the tests source file which is located in the `tests` directory
29and remove the `#[ignore]` flag from the next test and get the tests to pass
30again. Each separate test is a function with `#[test]` flag above it.
31Continue, until you pass every test.
32
33If you wish to run all ignored tests without editing the tests source file, use:
34
35```bash
36$ cargo test -- --ignored
37```
38
39To run a specific test, for example `some_test`, you can use:
40
41```bash
42$ cargo test some_test
43```
44
45If the specific test is ignored use:
46
47```bash
48$ cargo test some_test -- --ignored
49```
50
51To learn more about Rust tests refer to the [online test documentation][rust-tests]
52
53Make sure to read the [Modules][modules] chapter if you
54haven't already, it will help you with organizing your files.
55
56## Further improvements
57
58After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
59
60To format your solution, inside the solution directory use
61
62```bash
63cargo fmt
64```
65
66To see, if your solution contains some common ineffective use cases, inside the solution directory use
67
68```bash
69cargo clippy --all-targets
70```
71
72## Submitting the solution
73
74Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
75
76## Feedback, Issues, Pull Requests
77
78The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
79
80If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
81
82[help-page]: https://exercism.io/tracks/rust/learning
83[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
84[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
85[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
86
87## Source
88
89A variation on Problem 7 at Project Euler [http://projecteuler.net/problem=7](http://projecteuler.net/problem=7)
90
91## Submitting Incomplete Solutions
92It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/nth-prime/src/lib.rs b/nth-prime/src/lib.rs
new file mode 100644
index 0000000..ef9d7ea
--- /dev/null
+++ b/nth-prime/src/lib.rs
@@ -0,0 +1,8 @@
1fn is_prime(n: u32) -> bool {
2 let limit = (n as f64).sqrt() as u32;
3 (2..=limit).all(|x| n % x != 0)
4}
5
6pub fn nth(n: u32) -> u32 {
7 (2..).filter(|x| is_prime(*x)).nth(n as usize).unwrap()
8}
diff --git a/nth-prime/tests/nth-prime.rs b/nth-prime/tests/nth-prime.rs
new file mode 100644
index 0000000..b313ff2
--- /dev/null
+++ b/nth-prime/tests/nth-prime.rs
@@ -0,0 +1,21 @@
1use nth_prime as np;
2
3#[test]
4fn test_first_prime() {
5 assert_eq!(np::nth(0), 2);
6}
7
8#[test]
9fn test_second_prime() {
10 assert_eq!(np::nth(1), 3);
11}
12
13#[test]
14fn test_sixth_prime() {
15 assert_eq!(np::nth(5), 13);
16}
17
18#[test]
19fn test_big_prime() {
20 assert_eq!(np::nth(10_000), 104_743);
21}