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

Makes nsieve fair. Solves #419 #431

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions bench/algorithm/nsieve/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// The Computer Language Shootout
// http://shootout.alioth.debian.org/
// Precedent C entry modified by bearophile for speed and size, 31 Jan 2006
// Converted to C++ by Paul Kitchin

#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

inline void nsieve(unsigned int limit) {
std::vector<bool> primes(limit + 1, true);
primes[1] = false;
unsigned int p = 2;
while (p <= static_cast<unsigned int>(std::sqrt(limit))) {
for (unsigned int i = p * p; i <= limit; i += p) {
primes[i] = false;
}
p = std::find(primes.begin() + p + 1, primes.end(), true) - primes.begin();
}
unsigned int primeCount = std::count(primes.begin(), primes.end(), true);
std::cout << "Primes up to " << std::setw(8) << limit << std::setw(9) << primeCount << '\n';
}

int main(int argc, char **argv) {
unsigned int n = (argc <= 1) ? 4 : std::atoi(argv[1]);
for (unsigned int i = n; i >= n - 2; --i) {
nsieve(10000 << i);
}
return 0;
}
1 change: 1 addition & 0 deletions bench/bench_cpp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ problems:
- name: nsieve
source:
- 1.cpp
- 3.cpp
compiler_version_command:
compiler_version_regex:
runtime_version_parameter:
Expand Down