Skip to content

Commit fd6a691

Browse files
authored
Add prime-factors (exercism#320)
1 parent ec51228 commit fd6a691

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

Diff for: config.json

+8
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@
334334
"transforming"
335335
]
336336
},
337+
{
338+
"slug": "prime-factors",
339+
"name": "Prime Factors",
340+
"uuid": "858b9e88-5c1c-48c7-9888-596d47a01d6e",
341+
"practices": [],
342+
"prerequisites": [],
343+
"difficulty": 2
344+
},
337345
{
338346
"slug": "proverb",
339347
"name": "Proverb",
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Instructions
2+
3+
Compute the prime factors of a given natural number.
4+
5+
A prime number is only evenly divisible by itself and 1.
6+
7+
Note that 1 is not a prime number.
8+
9+
## Example
10+
11+
What are the prime factors of 60?
12+
13+
- Our first divisor is 2.
14+
2 goes into 60, leaving 30.
15+
- 2 goes into 30, leaving 15.
16+
- 2 doesn't go cleanly into 15.
17+
So let's move on to our next divisor, 3.
18+
- 3 goes cleanly into 15, leaving 5.
19+
- 3 does not go cleanly into 5.
20+
The next possible factor is 4.
21+
- 4 does not go cleanly into 5.
22+
The next possible factor is 5.
23+
- 5 does go cleanly into 5.
24+
- We're left only with 1, so now, we're done.
25+
26+
Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.
27+
28+
You can check this yourself:
29+
30+
```text
31+
2 * 2 * 3 * 5
32+
= 4 * 15
33+
= 60
34+
```
35+
36+
Success!

Diff for: exercises/practice/prime-factors/.meta/config.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"prime_factors.vim"
8+
],
9+
"test": [
10+
"prime_factors.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"blurb": "Compute the prime factors of a given natural number.",
17+
"source": "The Prime Factors Kata by Uncle Bob",
18+
"source_url": "https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata"
19+
}

Diff for: exercises/practice/prime-factors/.meta/example.vim

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function! Factors(value) abort
2+
let l:results = []
3+
let l:divisor = 2
4+
let l:working = a:value
5+
while l:working > 1
6+
while l:working % l:divisor == 0
7+
call add(l:results, l:divisor)
8+
let l:working /= l:divisor
9+
endwhile
10+
let l:divisor += 1
11+
endwhile
12+
13+
return l:results
14+
endfunction

Diff for: exercises/practice/prime-factors/.meta/tests.toml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
[924fc966-a8f5-4288-82f2-6b9224819ccd]
13+
description = "no factors"
14+
15+
[17e30670-b105-4305-af53-ddde182cb6ad]
16+
description = "prime number"
17+
18+
[238d57c8-4c12-42ef-af34-ae4929f94789]
19+
description = "another prime number"
20+
21+
[f59b8350-a180-495a-8fb1-1712fbee1158]
22+
description = "square of a prime"
23+
24+
[756949d3-3158-4e3d-91f2-c4f9f043ee70]
25+
description = "product of first prime"
26+
27+
[bc8c113f-9580-4516-8669-c5fc29512ceb]
28+
description = "cube of a prime"
29+
30+
[7d6a3300-a4cb-4065-bd33-0ced1de6cb44]
31+
description = "product of second prime"
32+
33+
[073ac0b2-c915-4362-929d-fc45f7b9a9e4]
34+
description = "product of third prime"
35+
36+
[6e0e4912-7fb6-47f3-a9ad-dbcd79340c75]
37+
description = "product of first and second prime"
38+
39+
[00485cd3-a3fe-4fbe-a64a-a4308fc1f870]
40+
description = "product of primes and non-primes"
41+
42+
[02251d54-3ca1-4a9b-85e1-b38f4b0ccb91]
43+
description = "product of primes"
44+
45+
[070cf8dc-e202-4285-aa37-8d775c9cd473]
46+
description = "factors include a large prime"

Diff for: exercises/practice/prime-factors/prime_factors.vader

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Execute (no factors):
2+
AssertEqual [], Factors(1)
3+
4+
Execute (prime number):
5+
AssertEqual [2], Factors(2)
6+
7+
Execute (another prime number):
8+
AssertEqual [3], Factors(3)
9+
10+
Execute (square of a prime):
11+
AssertEqual [3, 3], Factors(9)
12+
13+
Execute (product of first prime):
14+
AssertEqual [2, 2], Factors(4)
15+
16+
Execute (cube of a prime):
17+
AssertEqual [2, 2, 2], Factors(8)
18+
19+
Execute (product of second prime):
20+
AssertEqual [3, 3, 3], Factors(27)
21+
22+
Execute (product of third prime):
23+
AssertEqual [5, 5, 5, 5], Factors(625)
24+
25+
Execute (product of first and second prime):
26+
AssertEqual [2, 3], Factors(6)
27+
28+
Execute (product of primes and non-primes):
29+
AssertEqual [2, 2, 3], Factors(12)
30+
31+
Execute (product of primes):
32+
AssertEqual [5, 17, 23, 461], Factors(901255)
33+
34+
Execute (factors include a large prime):
35+
AssertEqual [11, 9539, 894119], Factors(93819012551)

Diff for: exercises/practice/prime-factors/prime_factors.vim

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"
2+
" Given a number, return its prime factors.
3+
"
4+
" Examples:
5+
" :echo Factors(1)
6+
" []
7+
"
8+
" :echo Factors(12)
9+
" [2, 2, 3]
10+
"
11+
function! Factors(value) abort
12+
" your code goes here
13+
endfunction

0 commit comments

Comments
 (0)