Skip to content

Commit ec51228

Browse files
authored
add nth-prime (#319)
1 parent e73248f commit ec51228

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@
291291
"prerequisites": [],
292292
"difficulty": 2
293293
},
294+
{
295+
"slug": "nth-prime",
296+
"name": "Nth Prime",
297+
"uuid": "30726f91-c0b9-47fc-8574-f1ff7e5179d3",
298+
"practices": [],
299+
"prerequisites": [],
300+
"difficulty": 2
301+
},
294302
{
295303
"slug": "pangram",
296304
"name": "Pangram",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
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 the 6th prime is 13.
6+
7+
If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"nth_prime.vim"
8+
],
9+
"test": [
10+
"nth_prime.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"blurb": "Given a number n, determine what the nth prime is.",
17+
"source": "A variation on Problem 7 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=7"
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function! Prime(number) abort
2+
if a:number == 0
3+
throw 'there is no zeroth prime'
4+
elseif a:number == 1
5+
return 2
6+
endif
7+
8+
let l:tally = 1
9+
let l:candidate = 1
10+
while l:tally < a:number
11+
let l:candidate += 2
12+
if IsPrime(l:candidate)
13+
let l:tally += 1
14+
endif
15+
endwhile
16+
17+
return l:candidate
18+
endfunction
19+
20+
function! IsPrime(n) abort
21+
if a:n <= 1
22+
return 0
23+
elseif a:n == 2
24+
return 1
25+
elseif a:n % 2 == 0
26+
return 0
27+
endif
28+
29+
for l:i in range(3, float2nr(sqrt(a:n)) + 1, 2)
30+
if a:n % l:i == 0
31+
return 0
32+
endif
33+
endfor
34+
35+
return 1
36+
endfunction
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[75c65189-8aef-471a-81de-0a90c728160c]
13+
description = "first prime"
14+
15+
[2c38804c-295f-4701-b728-56dea34fd1a0]
16+
description = "second prime"
17+
18+
[56692534-781e-4e8c-b1f9-3e82c1640259]
19+
description = "sixth prime"
20+
21+
[fce1e979-0edb-412d-93aa-2c744e8f50ff]
22+
description = "big prime"
23+
24+
[bd0a9eae-6df7-485b-a144-80e13c7d55b2]
25+
description = "there is no zeroth prime"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Execute (first prime):
2+
let g:number = 1
3+
let g:expected = 2
4+
AssertEqual g:expected, Prime(g:number)
5+
6+
Execute (second prime):
7+
let g:number = 2
8+
let g:expected = 3
9+
AssertEqual g:expected, Prime(g:number)
10+
11+
Execute (sixth prime):
12+
let g:number = 6
13+
let g:expected = 13
14+
AssertEqual g:expected, Prime(g:number)
15+
16+
Execute (big prime):
17+
let g:number = 10001
18+
let g:expected = 104743
19+
AssertEqual g:expected, Prime(g:number)
20+
21+
Execute (there is no zeroth prime):
22+
let g:number = 0
23+
let g:expected = 'there is no zeroth prime'
24+
AssertThrows call Prime(g:number)
25+
AssertEqual g:expected, g:vader_exception
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"
2+
" Given a number n, determine what the nth prime is.
3+
"
4+
" Example:
5+
"
6+
" :echo Prime(6)
7+
" 13
8+
"
9+
" :echo Prime(10001)
10+
" 104743
11+
"
12+
function! Prime(number) abort
13+
" your implementation goes here
14+
endfunction

0 commit comments

Comments
 (0)