Skip to content

Commit

Permalink
add nth-prime (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Oct 7, 2024
1 parent e73248f commit ec51228
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/nth-prime/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/nth-prime/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
36 changes: 36 additions & 0 deletions exercises/practice/nth-prime/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions exercises/practice/nth-prime/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions exercises/practice/nth-prime/nth_prime.vader
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions exercises/practice/nth-prime/nth_prime.vim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ec51228

Please sign in to comment.