Skip to content

Conversation

@jt0202
Copy link
Contributor

@jt0202 jt0202 commented Jul 29, 2025

This PR implements the solution for task 96 using a sieve construction. I currently struggle a bit with the best definition for the primes that make the proofs simple.

@jt0202 jt0202 marked this pull request as draft July 29, 2025 15:47
(eratosthenes_sieve.go n sieve 2).toList.mergeSort
where
go (n : Nat) (sieve : Std.HashSet Nat) (curr : Nat) :=
if curr < n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only go up to sqrt n if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not available without batteries. I could copy this over though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just check for curr * curr < n.

if curr < n
then
if curr ∈ sieve
then eratosthenes_sieve.go n (sieve.filter (fun x => (x = curr ∨ x % curr != 0))) (curr + 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this has the right asymptotic complexity. The filtering step takes time proportional to the number of remaining elements in sieve. In the usual sieve of Eratosthenes, you iterate from curr^2 to n with step size curr, so the time taken is proportional to something like (n - curr^2)/curr, which I think is faster.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I think that this is the same fundamental difference that is described in section 2 of https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants