Skip to content

Commit 178a3ed

Browse files
Add prime-factors exercise (#60)
1 parent 7e4f44c commit 178a3ed

File tree

11 files changed

+1099
-0
lines changed

11 files changed

+1099
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@
300300
"prerequisites": [],
301301
"difficulty": 3
302302
},
303+
{
304+
"slug": "prime-factors",
305+
"name": "Prime Factors",
306+
"uuid": "666dbc7f-a9a0-444b-9e3a-ebe9bc53fc6b",
307+
"practices": [],
308+
"prerequisites": [],
309+
"difficulty": 3
310+
},
303311
{
304312
"slug": "protein-translation",
305313
"name": "Protein Translation",
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+
"PrimeFactors.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
unit PrimeFactors;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TIntArray = Array Of Integer;
9+
10+
function factors(
11+
const value : integer
12+
) : TIntArray;
13+
14+
implementation
15+
16+
uses SysUtils;
17+
18+
function factors(
19+
const value : integer
20+
) : TIntArray;
21+
var
22+
n : integer;
23+
p : integer = 2;
24+
begin
25+
result := [];
26+
27+
n := value;
28+
while n > 1 do
29+
if n mod p <> 0 then
30+
p := p + 1
31+
else
32+
begin
33+
insert(p, result, length(result));
34+
n := n div p;
35+
end;
36+
end;
37+
38+
end.
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
unit PrimeFactors;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TIntArray = Array Of Integer;
9+
10+
function factors(
11+
const value : integer
12+
) : TIntArray;
13+
14+
implementation
15+
16+
uses SysUtils;
17+
18+
function factors(
19+
const value : integer
20+
) : TIntArray;
21+
begin
22+
23+
raise ENotImplemented.Create('Please implement your solution.'); result := [value];
24+
25+
end;
26+
27+
end.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
unit TestCases;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils;
8+
9+
type
10+
PrimeFactorsTest = class(TTestCase)
11+
published
12+
procedure no_factors;
13+
procedure prime_number;
14+
procedure another_prime_number;
15+
procedure square_of_a_prime;
16+
procedure product_of_first_prime;
17+
procedure cube_of_a_prime;
18+
procedure product_of_second_prime;
19+
procedure product_of_third_prime;
20+
procedure product_of_first_and_second_prime;
21+
procedure product_of_primes_and_non_primes;
22+
procedure product_of_primes;
23+
end;
24+
25+
implementation
26+
27+
uses PrimeFactors;
28+
29+
// 924fc966-a8f5-4288-82f2-6b9224819ccd
30+
procedure PrimeFactorsTest.no_factors;
31+
var
32+
expect : TIntArray;
33+
actual : TIntArray;
34+
begin
35+
expect := [];
36+
actual := PrimeFactors.factors(1);
37+
TapAssertTrue(Self, 'no factors', expect, actual);
38+
end;
39+
40+
// 17e30670-b105-4305-af53-ddde182cb6ad
41+
procedure PrimeFactorsTest.prime_number;
42+
var
43+
expect : TIntArray;
44+
actual : TIntArray;
45+
begin
46+
expect := [2];
47+
actual := PrimeFactors.factors(2);
48+
TapAssertTrue(Self, 'prime number', expect, actual);
49+
end;
50+
51+
// 238d57c8-4c12-42ef-af34-ae4929f94789
52+
procedure PrimeFactorsTest.another_prime_number;
53+
var
54+
expect : TIntArray;
55+
actual : TIntArray;
56+
begin
57+
expect := [3];
58+
actual := PrimeFactors.factors(3);
59+
TapAssertTrue(Self, 'another prime number', expect, actual);
60+
end;
61+
62+
// f59b8350-a180-495a-8fb1-1712fbee1158
63+
procedure PrimeFactorsTest.square_of_a_prime;
64+
var
65+
expect : TIntArray;
66+
actual : TIntArray;
67+
begin
68+
expect := [3, 3];
69+
actual := PrimeFactors.factors(9);
70+
TapAssertTrue(Self, 'square of a prime', expect, actual);
71+
end;
72+
73+
// 756949d3-3158-4e3d-91f2-c4f9f043ee70
74+
procedure PrimeFactorsTest.product_of_first_prime;
75+
var
76+
expect : TIntArray;
77+
actual : TIntArray;
78+
begin
79+
expect := [2, 2];
80+
actual := PrimeFactors.factors(4);
81+
TapAssertTrue(Self, 'product of first prime', expect, actual);
82+
end;
83+
84+
// bc8c113f-9580-4516-8669-c5fc29512ceb
85+
procedure PrimeFactorsTest.cube_of_a_prime;
86+
var
87+
expect : TIntArray;
88+
actual : TIntArray;
89+
begin
90+
expect := [2, 2, 2];
91+
actual := PrimeFactors.factors(8);
92+
TapAssertTrue(Self, 'cube of a prime', expect, actual);
93+
end;
94+
95+
// 7d6a3300-a4cb-4065-bd33-0ced1de6cb44
96+
procedure PrimeFactorsTest.product_of_second_prime;
97+
var
98+
expect : TIntArray;
99+
actual : TIntArray;
100+
begin
101+
expect := [3, 3, 3];
102+
actual := PrimeFactors.factors(27);
103+
TapAssertTrue(Self, 'product of second prime', expect, actual);
104+
end;
105+
106+
// 073ac0b2-c915-4362-929d-fc45f7b9a9e4
107+
procedure PrimeFactorsTest.product_of_third_prime;
108+
var
109+
expect : TIntArray;
110+
actual : TIntArray;
111+
begin
112+
expect := [5, 5, 5, 5];
113+
actual := PrimeFactors.factors(625);
114+
TapAssertTrue(Self, 'product of third prime', expect, actual);
115+
end;
116+
117+
// 6e0e4912-7fb6-47f3-a9ad-dbcd79340c75
118+
procedure PrimeFactorsTest.product_of_first_and_second_prime;
119+
var
120+
expect : TIntArray;
121+
actual : TIntArray;
122+
begin
123+
expect := [2, 3];
124+
actual := PrimeFactors.factors(6);
125+
TapAssertTrue(Self, 'product of first and second prime', expect, actual);
126+
end;
127+
128+
// 00485cd3-a3fe-4fbe-a64a-a4308fc1f870
129+
procedure PrimeFactorsTest.product_of_primes_and_non_primes;
130+
var
131+
expect : TIntArray;
132+
actual : TIntArray;
133+
begin
134+
expect := [2, 2, 3];
135+
actual := PrimeFactors.factors(12);
136+
TapAssertTrue(Self, 'product of primes and non-primes', expect, actual);
137+
end;
138+
139+
// 02251d54-3ca1-4a9b-85e1-b38f4b0ccb91
140+
procedure PrimeFactorsTest.product_of_primes;
141+
var
142+
expect : TIntArray;
143+
actual : TIntArray;
144+
begin
145+
expect := [5, 17, 23, 461];
146+
actual := PrimeFactors.factors(901255);
147+
TapAssertTrue(Self, 'product of primes', expect, actual);
148+
end;
149+
150+
initialization
151+
RegisterTest(PrimeFactorsTest);
152+
153+
end.

0 commit comments

Comments
 (0)