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

Problem on article Linear Sieve #1260

Open
ahmedosama20 opened this issue Apr 10, 2024 · 1 comment
Open

Problem on article Linear Sieve #1260

ahmedosama20 opened this issue Apr 10, 2024 · 1 comment

Comments

@ahmedosama20
Copy link

Article: Linear Sieve

Problem:

It is possible to calculate the primes without saving an array of the least prime factors thus making the memory constraints of the algorithm the same as the classic sieve:

vector<bool> lp(N+1);
...
if (lp[i] == false) {
    pr.push_back(i);
}
for (int j = 0; i * pr[j] <= N; ++j) {
    lp[i * pr[j]] = true;
    if (i%pr[j] == 0) {
        break;
    }
}
@adamant-pwn
Copy link
Member

Thanks for the comment! Wouldn't it be slower on practice though? I imagine i % pr[j] operation would be much costlier than pr[j] == lp[i]. I guess we could still mention that it is an option, if you want to make a pull request about it, but I wouldn't use it as the main implementation?

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

No branches or pull requests

2 participants