Skip to content

Commit bd94048

Browse files
authored
Add sieve exercise (#514)
* Add sieve exercise * Update to use template
1 parent c134aa2 commit bd94048

File tree

14 files changed

+207
-17
lines changed

14 files changed

+207
-17
lines changed

config.json

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
"solution": [
2323
"%{snake_slug}.ml"
2424
],
25-
"editor": [
26-
"%{snake_slug}.mli"
27-
],
2825
"test": [
2926
"test.ml"
3027
],
@@ -33,10 +30,12 @@
3330
],
3431
"exemplar": [
3532
".meta/exemplar.ml"
33+
],
34+
"editor": [
35+
"%{snake_slug}.mli"
3636
]
3737
},
3838
"exercises": {
39-
"concept": [],
4039
"practice": [
4140
{
4241
"slug": "hexadecimal",
@@ -45,7 +44,6 @@
4544
"practices": [],
4645
"prerequisites": [],
4746
"difficulty": 1,
48-
"topics": null,
4947
"status": "deprecated"
5048
},
5149
{
@@ -111,8 +109,7 @@
111109
"uuid": "dd066b62-c63e-467c-8dfb-435bcc81d532",
112110
"practices": [],
113111
"prerequisites": [],
114-
"difficulty": 2,
115-
"topics": null
112+
"difficulty": 2
116113
},
117114
{
118115
"slug": "reverse-string",
@@ -240,8 +237,7 @@
240237
"uuid": "a43c28f4-8f5c-40f7-90cb-79689c810f95",
241238
"practices": [],
242239
"prerequisites": [],
243-
"difficulty": 3,
244-
"topics": []
240+
"difficulty": 3
245241
},
246242
{
247243
"slug": "all-your-base",
@@ -284,8 +280,7 @@
284280
"uuid": "8d21e4be-5e9c-4c73-81c5-b4ee2517d9c7",
285281
"practices": [],
286282
"prerequisites": [],
287-
"difficulty": 4,
288-
"topics": []
283+
"difficulty": 4
289284
},
290285
{
291286
"slug": "matching-brackets",
@@ -546,10 +541,17 @@
546541
"topics": [
547542
"functional_programming"
548543
]
544+
},
545+
{
546+
"slug": "sieve",
547+
"name": "Sieve",
548+
"uuid": "8157f71a-25ca-476c-8ac8-dcf660e5193b",
549+
"practices": [],
550+
"prerequisites": [],
551+
"difficulty": 2
549552
}
550553
]
551554
},
552-
"concepts": [],
553555
"key_features": [
554556
{
555557
"title": "Algebraic data types",
@@ -583,14 +585,14 @@
583585
}
584586
],
585587
"tags": [
586-
"paradigm/functional",
587-
"typing/static",
588-
"typing/strong",
589588
"execution_mode/compiled",
590-
"platform/windows",
591-
"platform/mac",
589+
"paradigm/functional",
592590
"platform/linux",
591+
"platform/mac",
592+
"platform/windows",
593593
"runtime/language_specific",
594+
"typing/static",
595+
"typing/strong",
594596
"used_for/financial_systems",
595597
"used_for/scientific_calculations"
596598
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Instructions
2+
3+
Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find all prime numbers less than or equal to a given number.
4+
5+
A prime number is a number larger than 1 that is only divisible by 1 and itself.
6+
For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
7+
By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3.
8+
9+
To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number.
10+
Then you repeat the following steps:
11+
12+
1. Find the next unmarked number in your list (skipping over marked numbers).
13+
This is a prime number.
14+
2. Mark all the multiples of that prime number as **not** prime.
15+
16+
You keep repeating these steps until you've gone through every number in your list.
17+
At the end, all the unmarked numbers are prime.
18+
19+
~~~~exercism/note
20+
The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes.
21+
To check you are implementing the Sieve correctly, a good first test is to check that you do not use division or remainder operations.
22+
~~~~
23+
24+
## Example
25+
26+
Let's say you're finding the primes less than or equal to 10.
27+
28+
- List out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked.
29+
- 2 is unmarked and is therefore a prime.
30+
Mark 4, 6, 8 and 10 as "not prime".
31+
- 3 is unmarked and is therefore a prime.
32+
Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_.
33+
- 4 is marked as "not prime", so we skip over it.
34+
- 5 is unmarked and is therefore a prime.
35+
Mark 10 as not prime _(optional - as it's already been marked)_.
36+
- 6 is marked as "not prime", so we skip over it.
37+
- 7 is unmarked and is therefore a prime.
38+
- 8 is marked as "not prime", so we skip over it.
39+
- 9 is marked as "not prime", so we skip over it.
40+
- 10 is marked as "not prime", so we stop as there are no more numbers to check.
41+
42+
You've examined all numbers and found 2, 3, 5, and 7 are still unmarked, which means they're the primes less than or equal to 10.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Introduction
2+
3+
You bought a big box of random computer parts at a garage sale.
4+
You've started putting the parts together to build custom computers.
5+
6+
You want to test the performance of different combinations of parts, and decide to create your own benchmarking program to see how your computers compare.
7+
You choose the famous "Sieve of Eratosthenes" algorithm, an ancient algorithm, but one that should push your computers to the limits.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"kahgoh"
4+
],
5+
"files": {
6+
"solution": [
7+
"sieve.ml"
8+
],
9+
"test": [
10+
"test.ml"
11+
],
12+
"example": [
13+
".meta/example.ml"
14+
]
15+
},
16+
"blurb": "Use the Sieve of Eratosthenes to find all the primes from 2 up to a given number.",
17+
"source": "Sieve of Eratosthenes at Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module IntSet = Set.Make (Int)
2+
3+
let rec mark (start : int) (step : int) (stop : int) (composites : IntSet.t) =
4+
match (start, step, stop, composites) with
5+
| start, _step, stop, composites when start > stop -> composites
6+
| start, step, stop, composites when IntSet.mem start composites ->
7+
mark (start + step) step stop composites
8+
| start, step, stop, composites ->
9+
mark (start + step) step stop (IntSet.add start composites)
10+
11+
let rec sift (start : int) (stop : int) (acc : int list) (composites : IntSet.t)
12+
=
13+
match (start, stop, acc, composites) with
14+
| start, stop, acc, _composites when start > stop -> acc
15+
| start, stop, acc, composites when IntSet.mem start composites ->
16+
sift (start + 1) stop acc composites
17+
| start, stop, acc, composites ->
18+
sift (start + 1) stop (start :: acc)
19+
(mark (start * 2) start stop composites)
20+
21+
let primes (limit : int) =
22+
match limit with
23+
| limit when limit < 2 -> []
24+
| limit -> sift 2 limit [] IntSet.empty
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+
[88529125-c4ce-43cc-bb36-1eb4ddd7b44f]
13+
description = "no primes under two"
14+
15+
[4afe9474-c705-4477-9923-840e1024cc2b]
16+
description = "find first prime"
17+
18+
[974945d8-8cd9-4f00-9463-7d813c7f17b7]
19+
description = "find primes up to 10"
20+
21+
[2e2417b7-3f3a-452a-8594-b9af08af6d82]
22+
description = "limit is prime"
23+
24+
[92102a05-4c7c-47de-9ed0-b7d5fcd00f21]
25+
description = "find primes up to 1000"

exercises/practice/sieve/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
default: clean test
2+
3+
test:
4+
dune runtest
5+
6+
clean:
7+
dune clean
8+
9+
.PHONY: clean

exercises/practice/sieve/dune

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(executable
2+
(name test)
3+
(libraries base ounit2))
4+
5+
(alias
6+
(name runtest)
7+
(deps (:x test.exe))
8+
(action (run %{x})))
9+
10+
(alias
11+
(name buildtest)
12+
(deps (:x test.exe)))
13+
14+
(env
15+
(dev
16+
(flags (:standard -warn-error -A))))

exercises/practice/sieve/dune-project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(lang dune 1.1)
2+
(version 1.0)

exercises/practice/sieve/sieve.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let primes _ =
2+
failwith "'primes' is missing"

0 commit comments

Comments
 (0)