Skip to content

Commit a9b457e

Browse files
Add sieve exercise (#62)
1 parent 775f1ea commit a9b457e

File tree

8 files changed

+201
-0
lines changed

8 files changed

+201
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@
321321
"prerequisites": [],
322322
"difficulty": 4
323323
},
324+
{
325+
"slug": "sieve",
326+
"name": "Sieve",
327+
"uuid": "105acc5d-dc8b-4fb9-b8f3-5faab3990954",
328+
"practices": [],
329+
"prerequisites": [],
330+
"difficulty": 4
331+
},
324332
{
325333
"slug": "triangle",
326334
"name": "Triangle",
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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, first, write out all the numbers from 2 up to and including your given number.
10+
Then, follow these steps:
11+
12+
1. Find the next unmarked number (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+
Repeat the steps until you've gone through every number.
17+
At the end, all the unmarked numbers are prime.
18+
19+
~~~~exercism/note
20+
The Sieve of Eratosthenes marks off multiples of each prime using addition (repeatedly adding the prime) or multiplication (directly computing its multiples), rather than checking each number for divisibility.
21+
22+
The tests don't check that you've implemented the algorithm, only that you've come up with the correct primes.
23+
~~~~
24+
25+
## Example
26+
27+
Let's say you're finding the primes less than or equal to 10.
28+
29+
- Write out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked.
30+
31+
```text
32+
2 3 4 5 6 7 8 9 10
33+
```
34+
35+
- 2 is unmarked and is therefore a prime.
36+
Mark 4, 6, 8 and 10 as "not prime".
37+
38+
```text
39+
2 3 [4] 5 [6] 7 [8] 9 [10]
40+
41+
```
42+
43+
- 3 is unmarked and is therefore a prime.
44+
Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_.
45+
46+
```text
47+
2 3 [4] 5 [6] 7 [8] [9] [10]
48+
49+
```
50+
51+
- 4 is marked as "not prime", so we skip over it.
52+
53+
```text
54+
2 3 [4] 5 [6] 7 [8] [9] [10]
55+
56+
```
57+
58+
- 5 is unmarked and is therefore a prime.
59+
Mark 10 as not prime _(optional - as it's already been marked)_.
60+
61+
```text
62+
2 3 [4] 5 [6] 7 [8] [9] [10]
63+
64+
```
65+
66+
- 6 is marked as "not prime", so we skip over it.
67+
68+
```text
69+
2 3 [4] 5 [6] 7 [8] [9] [10]
70+
71+
```
72+
73+
- 7 is unmarked and is therefore a prime.
74+
75+
```text
76+
2 3 [4] 5 [6] 7 [8] [9] [10]
77+
78+
```
79+
80+
- 8 is marked as "not prime", so we skip over it.
81+
82+
```text
83+
2 3 [4] 5 [6] 7 [8] [9] [10]
84+
85+
```
86+
87+
- 9 is marked as "not prime", so we skip over it.
88+
89+
```text
90+
2 3 [4] 5 [6] 7 [8] [9] [10]
91+
92+
```
93+
94+
- 10 is marked as "not prime", so we stop as there are no more numbers to check.
95+
96+
```text
97+
2 3 [4] 5 [6] 7 [8] [9] [10]
98+
99+
```
100+
101+
You've examined all the numbers and found that 2, 3, 5, and 7 are still unmarked, meaning 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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"sieve.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def primes (limit: i32): []i32 =
2+
let capacity = 1 + i64.i32 limit
3+
let (result, _, _) = loop (result, table, p) = ([], replicate capacity false, 2) while p < capacity do
4+
if table[p] then (result, table, p + 1) else
5+
let (table, _) = loop (table, multiple) = (table, p * p) while multiple < capacity do
6+
(table with [multiple] = true, multiple + p)
7+
in
8+
(result ++ [i32.i64 p], table, p + 1)
9+
in
10+
result
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/sieve.fut

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def primes (limit: i32): []i32 = ???

exercises/practice/sieve/test.fut

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import "sieve"
2+
3+
-- no primes under two
4+
-- ==
5+
-- input { 1 }
6+
-- output { empty([0]i32) }
7+
8+
-- find first prime
9+
-- ==
10+
-- input { 2 }
11+
-- output { [2] }
12+
13+
-- find primes up to 10
14+
-- ==
15+
-- input { 10 }
16+
-- output { [2, 3, 5, 7] }
17+
18+
-- limit is prime
19+
-- ==
20+
-- input { 13 }
21+
-- output { [2, 3, 5, 7, 11, 13] }
22+
23+
-- find primes up to 1000
24+
-- ==
25+
-- input { 1000 }
26+
-- output { [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] }
27+
28+
def main (limit: i32): []i32 =
29+
primes limit
30+

0 commit comments

Comments
 (0)