diff --git a/config.json b/config.json index de03fee8..282110eb 100644 --- a/config.json +++ b/config.json @@ -586,6 +586,14 @@ "prerequisites": [], "difficulty": 8 }, + { + "slug": "sieve", + "name": "Sieve", + "uuid": "285ad81c-e27b-4fe4-a2f6-36c9c502b62d", + "practices": [], + "prerequisites": [], + "difficulty": 8 + }, { "slug": "state-of-tic-tac-toe", "name": "State of Tic-Tac-Toe", diff --git a/exercises/practice/sieve/.docs/instructions.md b/exercises/practice/sieve/.docs/instructions.md new file mode 100644 index 00000000..71292e17 --- /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 00000000..f6c1cf79 --- /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 00000000..2f31ddd9 --- /dev/null +++ b/exercises/practice/sieve/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "jimmytty" + ], + "files": { + "solution": [ + "sieve.sql" + ], + "test": [ + "sieve_test.sql" + ], + "example": [ + ".meta/example.sql" + ] + }, + "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.sql b/exercises/practice/sieve/.meta/example.sql new file mode 100644 index 00000000..eafc9c13 --- /dev/null +++ b/exercises/practice/sieve/.meta/example.sql @@ -0,0 +1,53 @@ +WITH results ("limit", primes) AS ( +SELECT + "limit", + (WITH + cte AS ( + SELECT (SELECT JSON_GROUP_ARRAY(JSON_ARRAY(g.value, NULL)) + FROM GENERATE_SERIES(2, "limit") g) numbers + ) + SELECT + (WITH RECURSIVE rcte (matrix) AS ( + VALUES (numbers) + UNION ALL + SELECT + (WITH first_unmarked (prime) AS ( + SELECT JSON_EXTRACT(value, '$[0]') + FROM JSON_EACH(matrix) + WHERE JSON_EXTRACT(value, '$[1]') ISNULL + LIMIT 1 + ) + SELECT + JSON_GROUP_ARRAY( + CASE + WHEN JSON_EXTRACT(j.value, '$[0]') = prime + THEN JSON_SET(j.value, '$[1]', JSON('true')) + WHEN JSON_EXTRACT(j.value, '$[1]') ISNULL AND + JSON_EXTRACT(j.value, '$[0]') % prime = 0 + THEN JSON_SET(j.value, '$[1]', JSON('false')) + ELSE j.value + END + ) + FROM first_unmarked, JSON_EACH(matrix) j + ) + FROM rcte + WHERE (SELECT 1 + FROM JSON_EACH(matrix) + WHERE JSON_EXTRACT(value, '$[1]') ISNULL) + ) + SELECT COALESCE(GROUP_CONCAT(JSON_EXTRACT(j.value, '$[0]'), ', '), '') + FROM rcte, JSON_EACH(matrix) j + WHERE (SELECT COUNT(*) = 0 + FROM JSON_EACH(matrix) + WHERE JSON_EXTRACT(value, '$[1]') ISNULL) + AND JSON_EXTRACT(j.value, '$[1]') + ) + FROM cte + ) + FROM sieve +) +UPDATE sieve + SET result = primes + FROM results + WHERE sieve."limit" = results."limit" +; diff --git a/exercises/practice/sieve/.meta/tests.toml b/exercises/practice/sieve/.meta/tests.toml new file mode 100644 index 00000000..fec5e1a1 --- /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/create_fixture.sql b/exercises/practice/sieve/create_fixture.sql new file mode 100644 index 00000000..2ddf0eda --- /dev/null +++ b/exercises/practice/sieve/create_fixture.sql @@ -0,0 +1,10 @@ +DROP TABLE IF EXISTS sieve; +CREATE TABLE sieve ( + "limit" INTEGER NOT NULL, + result TEXT +); + +.mode csv +.import ./data.csv sieve + +UPDATE sieve SET result = NULL; diff --git a/exercises/practice/sieve/create_test_table.sql b/exercises/practice/sieve/create_test_table.sql new file mode 100644 index 00000000..79c75f3b --- /dev/null +++ b/exercises/practice/sieve/create_test_table.sql @@ -0,0 +1,23 @@ +DROP TABLE IF EXISTS tests; +CREATE TABLE IF NOT EXISTS tests ( + -- uuid and description are taken from the test.toml file + uuid TEXT PRIMARY KEY, + description TEXT NOT NULL, + -- The following section is needed by the online test-runner + status TEXT DEFAULT 'fail', + message TEXT, + output TEXT, + test_code TEXT, + task_id INTEGER DEFAULT NULL, + -- Here are columns for the actual tests + "limit" INTEGER NOT NULL, + expected TEXT NOT NULL +); + +INSERT INTO tests (uuid, description, "limit", expected) +VALUES +('88529125-c4ce-43cc-bb36-1eb4ddd7b44f', 'no primes under two', 1, ''), +('4afe9474-c705-4477-9923-840e1024cc2b', 'find first prime', 2, '2'), +('974945d8-8cd9-4f00-9463-7d813c7f17b7', 'find primes up to 10', 10, '2, 3, 5, 7'), +('2e2417b7-3f3a-452a-8594-b9af08af6d82', 'limit is prime', 13, '2, 3, 5, 7, 11, 13'), +('92102a05-4c7c-47de-9ed0-b7d5fcd00f21', 'find primes up to 1000', 1000, '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'); diff --git a/exercises/practice/sieve/data.csv b/exercises/practice/sieve/data.csv new file mode 100644 index 00000000..8f21d755 --- /dev/null +++ b/exercises/practice/sieve/data.csv @@ -0,0 +1,5 @@ +1, +2, +10, +13, +1000, diff --git a/exercises/practice/sieve/sieve.sql b/exercises/practice/sieve/sieve.sql new file mode 100644 index 00000000..83fb58d0 --- /dev/null +++ b/exercises/practice/sieve/sieve.sql @@ -0,0 +1,8 @@ +-- Schema: +-- CREATE TABLE sieve ( +-- "limit" INTEGER NOT NULL, +-- result TEXT +-- ); +-- +-- Task: update the sieve table and set the result column based on the limit. +-- The result column must contain a string of prime numbers separated by ", ". For example, "2, 3". diff --git a/exercises/practice/sieve/sieve_test.sql b/exercises/practice/sieve/sieve_test.sql new file mode 100644 index 00000000..2d06c2b7 --- /dev/null +++ b/exercises/practice/sieve/sieve_test.sql @@ -0,0 +1,52 @@ +-- Create database: +.read ./create_fixture.sql + +-- Read user student solution and save any output as markdown in user_output.md: +.mode markdown +.output user_output.md +.read ./sieve.sql +.output + +-- Create a clean testing environment: +.read ./create_test_table.sql + +-- Comparison of user input and the tests updates the status for each test: +UPDATE tests + SET status = 'pass' + FROM (SELECT "limit", result FROM sieve) AS actual + WHERE (actual."limit", actual.result) = (tests."limit", tests.expected) +; + +-- Update message for failed tests to give helpful information: +UPDATE tests + SET message = ( + 'Result for "' || tests."limit" || '"' || ' is <' || + COALESCE(actual.result, 'NULL') || '> but should be <' || + tests.expected || '>' + ) + FROM (SELECT "limit", result FROM sieve) AS actual + WHERE actual."limit" = tests."LIMIT" + AND tests.status = 'fail' +; + +-- Save results to ./output.json (needed by the online test-runner) +.mode json +.once './output.json' +SELECT + description, + status, + message, + output, + test_code, + task_id +FROM + tests; + +-- Display test results in readable form for the student: +.mode table +SELECT + description, + status, + message +FROM + tests;