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 #409

Merged
merged 6 commits into from
Feb 15, 2024
Merged

Sieve #409

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 88 additions & 126 deletions config.json

Large diffs are not rendered by default.

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": [
"bcpeinhardt"
],
"files": {
"solution": [
"src/sieve.gleam"
],
"test": [
"test/sieve_test.gleam"
],
"example": [
".meta/example.gleam"
]
},
"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"
}
55 changes: 55 additions & 0 deletions exercises/practice/sieve/.meta/example.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import gleam/list
import gleam/iterator
import gleam/bool
import gleam/set.{type Set}

pub fn primes_up_to(upper_bound: Int) -> List(Int) {
// 2 is the first prime number. Smaller input values are invalid.
use <- bool.guard(when: upper_bound < 2, return: [])

// Generate a list of all integers from 2 up to the upper bound.
let possible_primes = list.range(from: 2, to: upper_bound)

// Run the sieve algorithm on the list of possible primes.
sieve([], set.new(), possible_primes, upper_bound)
|> list.reverse
}

fn sieve(
primes: List(Int),
composites: Set(Int),
candidates: List(Int),
upper_bound: Int,
) -> List(Int) {
case candidates {
// BASE CASE: If there are no more candidates, we're done.
[] -> primes

[next, ..rest] -> {
case set.contains(composites, next) {
// If the next candidate is a multiple of one of our discovered primes, skip it
True -> sieve(primes, composites, rest, upper_bound)

// The next candidate is not a multiple of any of our discovered primes, so it's prime.
False -> {
// Generate a list from n^2 to the upper bound of the multiples of
// our newly discovered prime.
let multiples =
iterator.iterate(next * next, fn(state) { state + next })
|> iterator.take_while(fn(n) { n <= upper_bound })
|> iterator.to_list
|> set.from_list

// Add the newly discovered prime to the list of primes, and
// add the multiples of the prime to the list of composites.
sieve(
[next, ..primes],
set.union(composites, multiples),
rest,
upper_bound,
)
}
}
}
}
}
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"
11 changes: 11 additions & 0 deletions exercises/practice/sieve/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "sieve"
version = "0.1.0"

[dependencies]
gleam_bitwise = "~> 1.2"
gleam_otp = "~> 0.7 or ~> 1.0"
gleam_stdlib = "~> 0.32 or ~> 1.0"
simplifile = "~> 1.0"

[dev-dependencies]
exercism_test_runner = "~> 1.4"
26 changes: 26 additions & 0 deletions exercises/practice/sieve/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.1", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "A6E9009E50BBE863EB37D963E4315398D41A3D87D0075480FC244125808F964A" },
{ name = "exercism_test_runner", version = "1.7.0", build_tools = ["gleam"], requirements = ["simplifile", "argv", "gleam_erlang", "gap", "glance", "gleam_stdlib", "gleam_community_ansi", "gleam_json"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "2FC1BADB19BEC2AE77BFD2D3A606A014C85412A7B874CAFC4BA8CF04B0B257CD" },
{ name = "gap", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "2EE1B0A17E85CF73A0C1D29DA315A2699117A8F549C8E8D89FA8261BE41EDEB1" },
{ name = "glance", version = "0.8.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "C486C920E1865F66A404AAB9A762C4226D95B60AC2C733D175B28C3F0920CE21" },
{ name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_colour"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "glexer", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "4484942A465482A0A100936E1E5F12314DB4B5AC0D87575A7B9E9062090B96BE" },
{ name = "simplifile", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "359CD7006E2F69255025C858CCC6407C11A876EC179E6ED1E46809E8DC6B1AAD" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

[requirements]
exercism_test_runner = { version = "~> 1.4" }
gleam_bitwise = { version = "~> 1.2" }
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
gleam_stdlib = { version = "~> 0.32 or ~> 1.0" }
simplifile = { version = "~> 1.0" }
3 changes: 3 additions & 0 deletions exercises/practice/sieve/src/sieve.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn primes_up_to(upper_bound: Int) -> List(Int) {
todo
}
50 changes: 50 additions & 0 deletions exercises/practice/sieve/test/sieve_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import exercism/test_runner
import exercism/should
import sieve

pub fn main() {
test_runner.main()
}

pub fn no_primes_under_two_test() {
sieve.primes_up_to(1)
|> should.equal([])
sieve.primes_up_to(0)
|> should.equal([])
sieve.primes_up_to(-1)
|> should.equal([])
sieve.primes_up_to(-20)
|> should.equal([])
}

pub fn find_first_prime_test() {
sieve.primes_up_to(2)
|> should.equal([2])
}

pub fn find_primes_up_to_10_test() {
sieve.primes_up_to(10)
|> should.equal([2, 3, 5, 7])
}

pub fn limit_is_prime_test() {
sieve.primes_up_to(13)
|> should.equal([2, 3, 5, 7, 11, 13])
}

pub fn find_primes_up_to_1000_test() {
sieve.primes_up_to(1000)
|> should.equal([
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,
])
}
Loading