Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sieve] Implementation #619

Merged
merged 4 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,16 @@
"prerequisites": [],
"difficulty": 1,
"topics": []
}
},
{
"slug": "sieve",
"name": "Sieve",
"uuid": "04a2cb52-8ebc-4b97-904b-f18fb9e5beda",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
}
]
},
"concepts": [],
Expand Down
28 changes: 28 additions & 0 deletions exercises/practice/sieve/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Instructions

Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find prime numbers.

A prime number is a number that is only divisible by 1 and itself.
For example, 2, 3, 5, 7, 11, and 13 are prime numbers.

The Sieve of Eratosthenes is an ancient algorithm that works by taking a list of numbers and crossing out all the numbers that aren't prime.

A number that is **not** prime is called a "composite number".

To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number.
Then you repeat the following steps:

1. Find the next unmarked number in your list. This is a prime number.
2. Mark all the multiples of that prime number as composite (not prime).

You keep repeating these steps until you've gone through every number in your list.
At the end, all the unmarked numbers are prime.

~~~~exercism/note
[Wikipedia's Sieve of Eratosthenes article][eratosthenes] has a useful graphic that explains the algorithm.

The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes.
A good first test is to check that you do not use division or remainder operations.

[eratosthenes]: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
~~~~
7 changes: 7 additions & 0 deletions exercises/practice/sieve/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/sieve/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"habere-et-dispertire"
],
"files": {
"solution": [
"Sieve.rakumod"
],
"test": [
"sieve.rakutest"
],
"example": [
".meta/solutions/Sieve.rakumod"
]
},
"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"
}
8 changes: 8 additions & 0 deletions exercises/practice/sieve/.meta/solutions/Sieve.rakumod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
unit module Sieve;

sub find-primes ( $limit ) is export {
sort keys [(^)] gather for 2 .. $limit
-> $root {
take { $root * ++$ } ...^ * > $limit
}
}
1 change: 1 addition & 0 deletions exercises/practice/sieve/.meta/solutions/sieve.rakutest
24 changes: 24 additions & 0 deletions exercises/practice/sieve/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
properties:
primes:
test: |-
sprintf(q:to/END/, (%case<input><limit>, %case<expected>.List<>, %case<description>).map(*.raku));
cmp-ok(
find-primes(%s),
"eq",
%s,
%s,
);
END

unit: module
example: |-
sub find-primes ( $limit ) is export {
sort keys [(^)] gather for 2 .. $limit
-> $root {
take { $root * ++$ } ...^ * > $limit
}
}

stub: |-
sub find-primes ( $number ) is export {
}
25 changes: 25 additions & 0 deletions exercises/practice/sieve/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions exercises/practice/sieve/Sieve.rakumod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unit module Sieve;

sub find-primes ( $number ) is export {
}
41 changes: 41 additions & 0 deletions exercises/practice/sieve/sieve.rakutest
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env raku
use Test;
use lib $?FILE.IO.dirname;
use Sieve;

cmp-ok( # begin: 88529125-c4ce-43cc-bb36-1eb4ddd7b44f
find-primes(1),
"eq",
(),
"no primes under two",
); # end: 88529125-c4ce-43cc-bb36-1eb4ddd7b44f

cmp-ok( # begin: 4afe9474-c705-4477-9923-840e1024cc2b
find-primes(2),
"eq",
(2,),
"find first prime",
); # end: 4afe9474-c705-4477-9923-840e1024cc2b

cmp-ok( # begin: 974945d8-8cd9-4f00-9463-7d813c7f17b7
find-primes(10),
"eq",
(2, 3, 5, 7),
"find primes up to 10",
); # end: 974945d8-8cd9-4f00-9463-7d813c7f17b7

cmp-ok( # begin: 2e2417b7-3f3a-452a-8594-b9af08af6d82
find-primes(13),
"eq",
(2, 3, 5, 7, 11, 13),
"limit is prime",
); # end: 2e2417b7-3f3a-452a-8594-b9af08af6d82

cmp-ok( # begin: 92102a05-4c7c-47de-9ed0-b7d5fcd00f21
find-primes(1000),
"eq",
habere-et-dispertire marked this conversation as resolved.
Show resolved Hide resolved
(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),
"find primes up to 1000",
); # end: 92102a05-4c7c-47de-9ed0-b7d5fcd00f21

done-testing;