Twin primes: Difference between revisions

Content added Content deleted
(Rust program allows limits to be specified on command line)
(C++ program allows limits to be specified on command line)
Line 104: Line 104:
<lang cpp>#include <cstdint>
<lang cpp>#include <cstdint>
#include <iostream>
#include <iostream>
#include <string>
#include <primesieve.hpp>
#include <primesieve.hpp>


void print_twin_prime_count(uint64_t limit) {
int main() {
std::cout << "Number of twin prime pairs less than " << limit
<< " is " << primesieve::count_twins(0, limit - 1) << '\n';
}

int main(int argc, char** argv) {
std::cout.imbue(std::locale(""));
std::cout.imbue(std::locale(""));
uint64_t limit = 10;
if (argc > 1) {
// print number of twin prime pairs less than limits specified
for (int power = 1; power < 12; ++power, limit *= 10) {
// on the command line
std::cout << "Number of twin prime pairs less than " << limit
<< " is " << primesieve::count_twins(0, limit) << '\n';
for (int i = 1; i < argc; ++i) {
try {
print_twin_prime_count(std::stoull(argv[i]));
} catch (const std::exception& ex) {
std::cerr << "Cannot parse limit from '" << argv[i] << "'\n";
}
}
} else {
// if no limit was specified then show the number of twin prime
// pairs less than powers of 10 up to 100 billion
uint64_t limit = 10;
for (int power = 1; power < 12; ++power, limit *= 10)
print_twin_prime_count(limit);
}
}
return 0;
return 0;