Skip to content

Commit d42c4e9

Browse files
Add prime-factors exercise (#45)
1 parent 2719b3b commit d42c4e9

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@
158158
"prerequisites": [],
159159
"difficulty": 3
160160
},
161+
{
162+
"slug": "prime-factors",
163+
"name": "Prime Factors",
164+
"uuid": "17b57129-15ca-49a6-80b9-19d7477bf590",
165+
"practices": [],
166+
"prerequisites": [],
167+
"difficulty": 3
168+
},
161169
{
162170
"slug": "raindrops",
163171
"name": "Raindrops",
Lines changed: 36 additions & 0 deletions
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!
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"prime_factors.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def factors (value: i32): []i32 =
2+
let (_, _, a, i) = loop (n, p, a, i) = (value, 2, replicate 32 0, 0) while n > 1 do
3+
if n % p != 0 then (n, p + 1, a, i) else
4+
(n / p, p, a with [i] = p, i + 1)
5+
in
6+
a[0:i]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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"
47+
include = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def factors (value: i32): []i32 = ???
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import "prime_factors"
2+
3+
-- no factors
4+
-- ==
5+
-- input { 1 }
6+
-- output { empty([0]i32) }
7+
8+
-- prime number
9+
-- ==
10+
-- input { 2 }
11+
-- output { [2] }
12+
13+
-- another prime number
14+
-- ==
15+
-- input { 3 }
16+
-- output { [3] }
17+
18+
-- square of a prime
19+
-- ==
20+
-- input { 9 }
21+
-- output { [3, 3] }
22+
23+
-- product of first prime
24+
-- ==
25+
-- input { 4 }
26+
-- output { [2, 2] }
27+
28+
-- cube of a prime
29+
-- ==
30+
-- input { 8 }
31+
-- output { [2, 2, 2] }
32+
33+
-- product of second prime
34+
-- ==
35+
-- input { 27 }
36+
-- output { [3, 3, 3] }
37+
38+
-- product of third prime
39+
-- ==
40+
-- input { 625 }
41+
-- output { [5, 5, 5, 5] }
42+
43+
-- product of first and second prime
44+
-- ==
45+
-- input { 6 }
46+
-- output { [2, 3] }
47+
48+
-- product of primes and non-primes
49+
-- ==
50+
-- input { 12 }
51+
-- output { [2, 2, 3] }
52+
53+
-- product of primes
54+
-- ==
55+
-- input { 901255 }
56+
-- output { [5, 17, 23, 461] }
57+
58+
let main (value: i32): []i32 =
59+
factors value

0 commit comments

Comments
 (0)