-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 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, 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 (skipping over marked numbers). | ||
This is a prime number. | ||
2. Mark all the multiples of that prime number as **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 | ||
The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes. | ||
To check you are implementing the Sieve correctly, a good first test is to check that you do not use division or remainder operations. | ||
~~~~ | ||
|
||
## Example | ||
|
||
Let's say you're finding the primes less than or equal to 10. | ||
|
||
- List out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked. | ||
- 2 is unmarked and is therefore a prime. | ||
Mark 4, 6, 8 and 10 as "not prime". | ||
- 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)_. | ||
- 4 is marked as "not prime", so we skip over it. | ||
- 5 is unmarked and is therefore a prime. | ||
Mark 10 as not prime _(optional - as it's already been marked)_. | ||
- 6 is marked as "not prime", so we skip over it. | ||
- 7 is unmarked and is therefore a prime. | ||
- 8 is marked as "not prime", so we skip over it. | ||
- 9 is marked as "not prime", so we skip over it. | ||
- 10 is marked as "not prime", so we stop as there are no more numbers to check. | ||
|
||
You've examined all numbers and found 2, 3, 5, and 7 are still unmarked, which means they're the primes less than or equal to 10. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module [primes] | ||
|
||
primes : U64 -> List U64 | ||
primes = \limit -> | ||
if limit < 2 then | ||
[] | ||
else | ||
|
||
help = \candidates, foundPrimes -> | ||
when candidates is | ||
[] -> foundPrimes | ||
[0, .. as rest] -> rest |> help foundPrimes | ||
[prime, .. as rest] -> | ||
List.range { start: After prime, end: At limit, step: prime } | ||
|> List.walk rest \filteredCandidates, multipleOfPrime -> | ||
filteredCandidates |> List.replace (multipleOfPrime - prime - 1) 0 |> .list | ||
|> help (foundPrimes |> List.append prime) | ||
help (List.range { start: At 2, end: At limit }) [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"ageron" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Sieve.roc" | ||
], | ||
"test": [ | ||
"sieve-test.roc" | ||
], | ||
"example": [ | ||
".meta/Example.roc" | ||
] | ||
}, | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{%- import "generator_macros.j2" as macros with context -%} | ||
{{ macros.canonical_ref() }} | ||
{{ macros.header() }} | ||
|
||
import {{ exercise | to_pascal }} exposing [{{ cases[0]["property"] | to_camel }}] | ||
|
||
{% for case in cases -%} | ||
# {{ case["description"] }} | ||
expect | ||
result = {{ case["property"] | to_camel }} {{ case["input"]["limit"] | to_roc }} | ||
result == {{ case["expected"] | to_roc }} | ||
|
||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module [primes] | ||
|
||
primes : U64 -> List U64 | ||
primes = \limit -> | ||
crash "Please implement the 'primes' function" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# These tests are auto-generated with test data from: | ||
# https://github.com/exercism/problem-specifications/tree/main/exercises/sieve/canonical-data.json | ||
# File last updated on 2024-09-15 | ||
app [main] { | ||
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br", | ||
} | ||
|
||
main = | ||
Task.ok {} | ||
|
||
import Sieve exposing [primes] | ||
|
||
# no primes under two | ||
expect | ||
result = primes 1 | ||
result == [] | ||
|
||
# find first prime | ||
expect | ||
result = primes 2 | ||
result == [2] | ||
|
||
# find primes up to 10 | ||
expect | ||
result = primes 10 | ||
result == [2, 3, 5, 7] | ||
|
||
# limit is prime | ||
expect | ||
result = primes 13 | ||
result == [2, 3, 5, 7, 11, 13] | ||
|
||
# find primes up to 1000 | ||
expect | ||
result = primes 1000 | ||
result == [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] | ||
|