From 2d672d2fef681ccb3cb5b77f624ff78d17a775fb Mon Sep 17 00:00:00 2001 From: vil02 Date: Sun, 3 Dec 2023 17:51:34 +0100 Subject: [PATCH 1/3] style: style: optimise `sieve_of_eratosthenes` --- .../math/prime/sieve_of_eratosthenes_spec.lua | 36 ++++++++++++++++--- src/math/prime/sieve_of_eratosthenes.lua | 5 +-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/.spec/math/prime/sieve_of_eratosthenes_spec.lua b/.spec/math/prime/sieve_of_eratosthenes_spec.lua index feb5938..b95b9cd 100644 --- a/.spec/math/prime/sieve_of_eratosthenes_spec.lua +++ b/.spec/math/prime/sieve_of_eratosthenes_spec.lua @@ -1,12 +1,33 @@ describe("Sieve of Eratosthenes", function() local sieve = require("math.prime.sieve_of_eratosthenes") local check_primality = require("math.prime.is_prime") - it("works for small numbers", function() - for n = 1, 5 do - for number, is_prime in ipairs(sieve(n ^ 2 * 1000)) do - assert.equal(check_primality(number), is_prime) - end + + local function check_sieve(n) + local sieve_result = sieve(n) + assert.equal(n, #sieve_result) + for number, is_prime in ipairs(sieve_result) do + assert.equal(check_primality(number), is_prime) end + end + + it("works for small numbers", function() + check_sieve(1) + check_sieve(2) + check_sieve(3) + check_sieve(4) + check_sieve(5) + check_sieve(6) + check_sieve(7) + check_sieve(9) + check_sieve(10) + check_sieve(24) + check_sieve(25) + check_sieve(26) + check_sieve(1000) + check_sieve(4000) + check_sieve(9000) + check_sieve(16000) + check_sieve(25000) end) it("yields the correct count for large numbers", function() local count = 0 @@ -17,4 +38,9 @@ describe("Sieve of Eratosthenes", function() end assert.equal(78498, count) end) + it("should throw error when input is not positive", function() + assert.has_error(function() + sieve(0) + end) + end) end) diff --git a/src/math/prime/sieve_of_eratosthenes.lua b/src/math/prime/sieve_of_eratosthenes.lua index 9729fee..cb01630 100644 --- a/src/math/prime/sieve_of_eratosthenes.lua +++ b/src/math/prime/sieve_of_eratosthenes.lua @@ -2,13 +2,14 @@ return function( n -- number ) + assert(n > 0, "The input must be positive") local is_prime = { false } for m = 2, n do -- mark as prime is_prime[m] = true end - for m = 2, n / 2 do -- iterate possible primes + for m = 2, math.sqrt(n) do -- iterate possible primes if is_prime[m] then - for l = 2 * m, n, m do -- iterate multiples + for l = m * m, n, m do -- iterate multiples is_prime[l] = false -- "cross out" composite end end From b01c99437a10b85bd3306fa11448be80898bce21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:50:04 +0100 Subject: [PATCH 2/3] Use loop in tests --- .spec/math/prime/sieve_of_eratosthenes_spec.lua | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/.spec/math/prime/sieve_of_eratosthenes_spec.lua b/.spec/math/prime/sieve_of_eratosthenes_spec.lua index b95b9cd..d2c9496 100644 --- a/.spec/math/prime/sieve_of_eratosthenes_spec.lua +++ b/.spec/math/prime/sieve_of_eratosthenes_spec.lua @@ -11,15 +11,9 @@ describe("Sieve of Eratosthenes", function() end it("works for small numbers", function() - check_sieve(1) - check_sieve(2) - check_sieve(3) - check_sieve(4) - check_sieve(5) - check_sieve(6) - check_sieve(7) - check_sieve(9) - check_sieve(10) + for i = 1, 10 do + check_sieve(i) + end check_sieve(24) check_sieve(25) check_sieve(26) From ade65cfd53c78fe31355fae48fbf6adbf1aeb0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:50:47 +0100 Subject: [PATCH 3/3] OCD (I should write a proper style guide...) --- src/math/prime/sieve_of_eratosthenes.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/prime/sieve_of_eratosthenes.lua b/src/math/prime/sieve_of_eratosthenes.lua index cb01630..33ceb18 100644 --- a/src/math/prime/sieve_of_eratosthenes.lua +++ b/src/math/prime/sieve_of_eratosthenes.lua @@ -2,7 +2,7 @@ return function( n -- number ) - assert(n > 0, "The input must be positive") + assert(n > 0, "n must be positive") local is_prime = { false } for m = 2, n do -- mark as prime is_prime[m] = true