diff --git a/config.json b/config.json index 14259b1..7e520b9 100644 --- a/config.json +++ b/config.json @@ -398,6 +398,14 @@ "practices": [], "prerequisites": [], "difficulty": 4 + }, + { + "slug": "sieve", + "name": "Sieve", + "uuid": "a78a4a9e-5187-4db4-a10a-e174dba78f4c", + "practices": [], + "prerequisites": [], + "difficulty": 4 } ] }, diff --git a/exercises/practice/sieve/.busted b/exercises/practice/sieve/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/sieve/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/sieve/.docs/instructions.md b/exercises/practice/sieve/.docs/instructions.md new file mode 100644 index 0000000..71292e1 --- /dev/null +++ b/exercises/practice/sieve/.docs/instructions.md @@ -0,0 +1,101 @@ +# Instructions + +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. + +A prime number is a number larger than 1 that is only divisible by 1 and itself. +For example, 2, 3, 5, 7, 11, and 13 are prime numbers. +By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3. + +To use the Sieve of Eratosthenes, first, write out all the numbers from 2 up to and including your given number. +Then, follow these steps: + +1. Find the next unmarked number (skipping over marked numbers). + This is a prime number. +2. Mark all the multiples of that prime number as **not** prime. + +Repeat the steps until you've gone through every number. +At the end, all the unmarked numbers are prime. + +~~~~exercism/note +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. + +The tests don't check that you've implemented the algorithm, only that you've come up with the correct primes. +~~~~ + +## Example + +Let's say you're finding the primes less than or equal to 10. + +- Write out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked. + + ```text + 2 3 4 5 6 7 8 9 10 + ``` + +- 2 is unmarked and is therefore a prime. + Mark 4, 6, 8 and 10 as "not prime". + + ```text + 2 3 [4] 5 [6] 7 [8] 9 [10] + ↑ + ``` + +- 3 is unmarked and is therefore a prime. + Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 4 is marked as "not prime", so we skip over it. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 5 is unmarked and is therefore a prime. + Mark 10 as not prime _(optional - as it's already been marked)_. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 6 is marked as "not prime", so we skip over it. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 7 is unmarked and is therefore a prime. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 8 is marked as "not prime", so we skip over it. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 9 is marked as "not prime", so we skip over it. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +- 10 is marked as "not prime", so we stop as there are no more numbers to check. + + ```text + 2 3 [4] 5 [6] 7 [8] [9] [10] + ↑ + ``` + +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. diff --git a/exercises/practice/sieve/.docs/introduction.md b/exercises/practice/sieve/.docs/introduction.md new file mode 100644 index 0000000..f6c1cf7 --- /dev/null +++ b/exercises/practice/sieve/.docs/introduction.md @@ -0,0 +1,7 @@ +# Introduction + +You bought a big box of random computer parts at a garage sale. +You've started putting the parts together to build custom computers. + +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. +You choose the famous "Sieve of Eratosthenes" algorithm, an ancient algorithm, but one that should push your computers to the limits. diff --git a/exercises/practice/sieve/.meta/config.json b/exercises/practice/sieve/.meta/config.json new file mode 100644 index 0000000..11c1e30 --- /dev/null +++ b/exercises/practice/sieve/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "sieve.moon" + ], + "test": [ + "sieve_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "blurb": "Use the Sieve of Eratosthenes to find all the primes from 2 up to a given number.", + "source": "Sieve of Eratosthenes at Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" +} diff --git a/exercises/practice/sieve/.meta/example.moon b/exercises/practice/sieve/.meta/example.moon new file mode 100644 index 0000000..d593bbe --- /dev/null +++ b/exercises/practice/sieve/.meta/example.moon @@ -0,0 +1,12 @@ +{ + primes: (limit) -> + flags = [true for _ = 1, limit] + flags[1] = false + + for i = 2, math.floor(math.sqrt limit) + if flags[i] + for j = i * i, limit, i + flags[j] = false + + [i for i = 2, limit when flags[i]] +} diff --git a/exercises/practice/sieve/.meta/spec_generator.moon b/exercises/practice/sieve/.meta/spec_generator.moon new file mode 100644 index 0000000..76743cd --- /dev/null +++ b/exercises/practice/sieve/.meta/spec_generator.moon @@ -0,0 +1,30 @@ +formatted = (list, level) -> + joined = table.concat list, ', ' + cmd = "echo '#{joined}' | fold -s -w 76" + fh = io.popen cmd, 'r' + return "{#{joined}}" if not fh + + lines = [indent(line\gsub('%s+$', ''), level + 1) for line in fh\lines!] + + result = {fh\close!} + lines = {joined} if not result[1] + + if #lines == 1 + "{#{lines[1]\gsub('^%s+', '')}}" + else + table.insert lines, 1, "{" + table.insert lines, (indent "}", level) + table.concat lines, '\n' + + +{ + module_name: 'Sieve', + + generate_test: (case, level) -> + lines = { + "result = Sieve.#{case.property} #{case.input.limit}", + "expected = #{formatted case.expected, level}", + "assert.are.same expected, result" + } + table.concat [indent line, level for line in *lines], '\n' +} diff --git a/exercises/practice/sieve/.meta/tests.toml b/exercises/practice/sieve/.meta/tests.toml new file mode 100644 index 0000000..fec5e1a --- /dev/null +++ b/exercises/practice/sieve/.meta/tests.toml @@ -0,0 +1,25 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[88529125-c4ce-43cc-bb36-1eb4ddd7b44f] +description = "no primes under two" + +[4afe9474-c705-4477-9923-840e1024cc2b] +description = "find first prime" + +[974945d8-8cd9-4f00-9463-7d813c7f17b7] +description = "find primes up to 10" + +[2e2417b7-3f3a-452a-8594-b9af08af6d82] +description = "limit is prime" + +[92102a05-4c7c-47de-9ed0-b7d5fcd00f21] +description = "find primes up to 1000" diff --git a/exercises/practice/sieve/sieve.moon b/exercises/practice/sieve/sieve.moon new file mode 100644 index 0000000..f011124 --- /dev/null +++ b/exercises/practice/sieve/sieve.moon @@ -0,0 +1,4 @@ +{ + primes: (limit) -> + error 'Implement me' +} diff --git a/exercises/practice/sieve/sieve_spec.moon b/exercises/practice/sieve/sieve_spec.moon new file mode 100644 index 0000000..fe144a7 --- /dev/null +++ b/exercises/practice/sieve/sieve_spec.moon @@ -0,0 +1,39 @@ +Sieve = require 'sieve' + +describe 'sieve', -> + it 'no primes under two', -> + result = Sieve.primes 1 + expected = {} + assert.are.same expected, result + + pending 'find first prime', -> + result = Sieve.primes 2 + expected = {2} + assert.are.same expected, result + + pending 'find primes up to 10', -> + result = Sieve.primes 10 + expected = {2, 3, 5, 7} + assert.are.same expected, result + + pending 'limit is prime', -> + result = Sieve.primes 13 + expected = {2, 3, 5, 7, 11, 13} + assert.are.same expected, result + + pending 'find primes up to 1000', -> + result = Sieve.primes 1000 + expected = { + 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 + } + assert.are.same expected, result