From fe283efdf737a017fd439638ed20291455affe70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:21:37 -0700 Subject: [PATCH] add `nth-prime` --- config.json | 8 +++++ .../practice/nth-prime/.docs/instructions.md | 7 ++++ .../practice/nth-prime/.meta/config.json | 19 ++++++++++ .../practice/nth-prime/.meta/example.vim | 36 +++++++++++++++++++ exercises/practice/nth-prime/.meta/tests.toml | 25 +++++++++++++ exercises/practice/nth-prime/nth_prime.vader | 25 +++++++++++++ exercises/practice/nth-prime/nth_prime.vim | 14 ++++++++ 7 files changed, 134 insertions(+) create mode 100644 exercises/practice/nth-prime/.docs/instructions.md create mode 100644 exercises/practice/nth-prime/.meta/config.json create mode 100644 exercises/practice/nth-prime/.meta/example.vim create mode 100644 exercises/practice/nth-prime/.meta/tests.toml create mode 100644 exercises/practice/nth-prime/nth_prime.vader create mode 100644 exercises/practice/nth-prime/nth_prime.vim diff --git a/config.json b/config.json index e684ced..2d80594 100644 --- a/config.json +++ b/config.json @@ -291,6 +291,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "nth-prime", + "name": "Nth Prime", + "uuid": "30726f91-c0b9-47fc-8574-f1ff7e5179d3", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "pangram", "name": "Pangram", diff --git a/exercises/practice/nth-prime/.docs/instructions.md b/exercises/practice/nth-prime/.docs/instructions.md new file mode 100644 index 0000000..065e323 --- /dev/null +++ b/exercises/practice/nth-prime/.docs/instructions.md @@ -0,0 +1,7 @@ +# Instructions + +Given a number n, determine 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. diff --git a/exercises/practice/nth-prime/.meta/config.json b/exercises/practice/nth-prime/.meta/config.json new file mode 100644 index 0000000..f041bca --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "nth_prime.vim" + ], + "test": [ + "nth_prime.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "blurb": "Given a number n, determine what the nth prime is.", + "source": "A variation on Problem 7 at Project Euler", + "source_url": "https://projecteuler.net/problem=7" +} diff --git a/exercises/practice/nth-prime/.meta/example.vim b/exercises/practice/nth-prime/.meta/example.vim new file mode 100644 index 0000000..c9bef66 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/example.vim @@ -0,0 +1,36 @@ +function! Prime(number) abort + if a:number == 0 + throw 'there is no zeroth prime' + elseif a:number == 1 + return 2 + endif + + let l:tally = 1 + let l:candidate = 1 + while l:tally < a:number + let l:candidate += 2 + if IsPrime(l:candidate) + let l:tally += 1 + endif + endwhile + + return l:candidate +endfunction + +function! IsPrime(n) abort + if a:n <= 1 + return 0 + elseif a:n == 2 + return 1 + elseif a:n % 2 == 0 + return 0 + endif + + for l:i in range(3, float2nr(sqrt(a:n)) + 1, 2) + if a:n % l:i == 0 + return 0 + endif + endfor + + return 1 +endfunction diff --git a/exercises/practice/nth-prime/.meta/tests.toml b/exercises/practice/nth-prime/.meta/tests.toml new file mode 100644 index 0000000..daccec4 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/tests.toml @@ -0,0 +1,25 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[75c65189-8aef-471a-81de-0a90c728160c] +description = "first prime" + +[2c38804c-295f-4701-b728-56dea34fd1a0] +description = "second prime" + +[56692534-781e-4e8c-b1f9-3e82c1640259] +description = "sixth prime" + +[fce1e979-0edb-412d-93aa-2c744e8f50ff] +description = "big prime" + +[bd0a9eae-6df7-485b-a144-80e13c7d55b2] +description = "there is no zeroth prime" diff --git a/exercises/practice/nth-prime/nth_prime.vader b/exercises/practice/nth-prime/nth_prime.vader new file mode 100644 index 0000000..98e9560 --- /dev/null +++ b/exercises/practice/nth-prime/nth_prime.vader @@ -0,0 +1,25 @@ +Execute (first prime): + let g:number = 1 + let g:expected = 2 + AssertEqual g:expected, Prime(g:number) + +Execute (second prime): + let g:number = 2 + let g:expected = 3 + AssertEqual g:expected, Prime(g:number) + +Execute (sixth prime): + let g:number = 6 + let g:expected = 13 + AssertEqual g:expected, Prime(g:number) + +Execute (big prime): + let g:number = 10001 + let g:expected = 104743 + AssertEqual g:expected, Prime(g:number) + +Execute (there is no zeroth prime): + let g:number = 0 + let g:expected = 'there is no zeroth prime' + AssertThrows call Prime(g:number) + AssertEqual g:expected, g:vader_exception diff --git a/exercises/practice/nth-prime/nth_prime.vim b/exercises/practice/nth-prime/nth_prime.vim new file mode 100644 index 0000000..55d17c5 --- /dev/null +++ b/exercises/practice/nth-prime/nth_prime.vim @@ -0,0 +1,14 @@ +" +" Given a number n, determine what the nth prime is. +" +" Example: +" +" :echo Prime(6) +" 13 +" +" :echo Prime(10001) +" 104743 +" +function! Prime(number) abort + " your implementation goes here +endfunction