Skip to content

feat: add prime sieve and generator#50

Closed
khreenberg wants to merge 1 commit into
TheAlgorithms:masterfrom
khreenberg:master
Closed

feat: add prime sieve and generator#50
khreenberg wants to merge 1 commit into
TheAlgorithms:masterfrom
khreenberg:master

Conversation

@khreenberg
Copy link
Copy Markdown

No description provided.

@zFl4wless
Copy link
Copy Markdown
Contributor

@appgurueu What about this PR? Seems like it's finished 🤔

Copy link
Copy Markdown
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

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

Looks like I forgot to submit my pending review 😅

Comment thread Maths/Prime.ts
* @returns All prime numbers from 2 through {@linkcode limit}
*/
export function sieveOfEratosthenes(limit: number): number[] {
assert(limit > 1, "limit should be an integer greater than 1");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: Replace all of this - including the assert def. - with if (!Number.isInteger(limit) || limit <= 1) throw new Error("limit should be an integer greater than 1");.

Comment thread Maths/Prime.ts

return maybePrime
.reduce(
(primes, isPrime, number) => (isPrime ? [...primes, number] : primes),
Copy link
Copy Markdown
Contributor

@appgurueu appgurueu Oct 11, 2022

Choose a reason for hiding this comment

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

This seems horribly inefficient (and also unreadable) to me: It always copies the entire primes array so far just to append a single value. This implies quadratic runtime in the number of primes. Please write it out imperatively:

const primes = [];
for (let i = 2; i < primes.length; i++) if (maybePrime[i]) primes.push(i);

which seems more concise, performant, and readable to me.

Copy link
Copy Markdown
Contributor

@zFl4wless zFl4wless Jul 9, 2023

Choose a reason for hiding this comment

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

I'm currently finishing this PR. There is a little spelling mistake in your suggestion. In the condition of the for-loop you typed primes instead of maybePrime :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm currently finishing this PR. There is a little spelling mistake in your suggestion. In the condition of the for-loop you typed primes instead of maybePrime :)

Indeed, that ought to be maybePrime.

@zFl4wless
Copy link
Copy Markdown
Contributor

Looks like I forgot to submit my pending review 😅

Happens ;) If you have the time, maybe you could go through the other PRs aswell. I think #97 has a Review pending too and maybe some can be sorted out. If people are no longer working on it, I will try Finishing their PRs.

@zFl4wless
Copy link
Copy Markdown
Contributor

Finished this PR in #144

@appgurueu appgurueu closed this Jul 9, 2023
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.

3 participants