Skip to content

Commit bb5f94f

Browse files
authored
sieve (#52)
1 parent 2e9c6d9 commit bb5f94f

File tree

10 files changed

+250
-0
lines changed

10 files changed

+250
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@
398398
"practices": [],
399399
"prerequisites": [],
400400
"difficulty": 4
401+
},
402+
{
403+
"slug": "sieve",
404+
"name": "Sieve",
405+
"uuid": "a78a4a9e-5187-4db4-a10a-e174dba78f4c",
406+
"practices": [],
407+
"prerequisites": [],
408+
"difficulty": 4
401409
}
402410
]
403411
},

exercises/practice/sieve/.busted

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"sieve.moon"
8+
],
9+
"test": [
10+
"sieve_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
primes: (limit) ->
3+
flags = [true for _ = 1, limit]
4+
flags[1] = false
5+
6+
for i = 2, math.floor(math.sqrt limit)
7+
if flags[i]
8+
for j = i * i, limit, i
9+
flags[j] = false
10+
11+
[i for i = 2, limit when flags[i]]
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
formatted = (list, level) ->
2+
joined = table.concat list, ', '
3+
cmd = "echo '#{joined}' | fold -s -w 76"
4+
fh = io.popen cmd, 'r'
5+
return "{#{joined}}" if not fh
6+
7+
lines = [indent(line\gsub('%s+$', ''), level + 1) for line in fh\lines!]
8+
9+
result = {fh\close!}
10+
lines = {joined} if not result[1]
11+
12+
if #lines == 1
13+
"{#{lines[1]\gsub('^%s+', '')}}"
14+
else
15+
table.insert lines, 1, "{"
16+
table.insert lines, (indent "}", level)
17+
table.concat lines, '\n'
18+
19+
20+
{
21+
module_name: 'Sieve',
22+
23+
generate_test: (case, level) ->
24+
lines = {
25+
"result = Sieve.#{case.property} #{case.input.limit}",
26+
"expected = #{formatted case.expected, level}",
27+
"assert.are.same expected, result"
28+
}
29+
table.concat [indent line, level for line in *lines], '\n'
30+
}
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"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
primes: (limit) ->
3+
error 'Implement me'
4+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Sieve = require 'sieve'
2+
3+
describe 'sieve', ->
4+
it 'no primes under two', ->
5+
result = Sieve.primes 1
6+
expected = {}
7+
assert.are.same expected, result
8+
9+
pending 'find first prime', ->
10+
result = Sieve.primes 2
11+
expected = {2}
12+
assert.are.same expected, result
13+
14+
pending 'find primes up to 10', ->
15+
result = Sieve.primes 10
16+
expected = {2, 3, 5, 7}
17+
assert.are.same expected, result
18+
19+
pending 'limit is prime', ->
20+
result = Sieve.primes 13
21+
expected = {2, 3, 5, 7, 11, 13}
22+
assert.are.same expected, result
23+
24+
pending 'find primes up to 1000', ->
25+
result = Sieve.primes 1000
26+
expected = {
27+
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
28+
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
29+
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
30+
239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
31+
331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
32+
421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
33+
509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
34+
613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
35+
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811,
36+
821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
37+
919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
38+
}
39+
assert.are.same expected, result

0 commit comments

Comments
 (0)