Strong and weak primes: Difference between revisions

Refactored C++ code
(Refactored C++ code)
Line 311:
#include "prime_sieve.hpp"
 
const int limit2limit1 = 100000001000000;
int main() {
const int limit1limit2 = 100000010000000;
const int limit2 = 10000000;
const int max_print[2] = { 36, 37 };
const int array_size = limit2 + 100;
 
class prime_info {
// find the prime numbers up to array_size
public:
prime_sieve sieve(array_size);
explicit prime_info(int max) : max_print(max) {}
 
void add_prime(int prime) {
}++count2;
if (prime < limit1)
++count1[index];
if (count2[index] <= max_print[index]) {
if (count2[index] > 1)
out[index] << ' ';
out[index] << p2prime;
}
}
void print(std::ostream& os, const char* name) const {
std::coutos << "First " << max_print[i] << " " << strength[i]name << " primes: " << out[i].str() << '\n';
std::coutos << "Number of " << strength[i]name << " primes below " << limit1 << ": " << count1[i] << '\n';
std::coutos << "Number of " << strength[i]name << " primes below " << limit2 << ": " << count2[i] << '\n';
}
private:
const int max_print[2] = { 36, 37 };
int count1[2] = { 0 };
int count2[2] = { 0 };
std::ostringstream out[2];
};
 
int main() {
prime_sieve sieve(array_sizelimit2 + 100);
 
// write numbers with groups of digits separated according to the system default locale
std::cout.imbue(std::locale(""));
std::cout << std::fixed;
 
// count and print strong/weak prime numbers
prime_info strong_primes(36);
int count1[2] = { 0 };
prime_info weak_primes(37);
int count2[2] = { 0 };
std::ostringstream out[2];
const char* strength[2] = { "strong", "weak" };
int p1 = 2, p2 = 3;
for (int p3 = 5; p2 < limit2; ++p3) {
Line 334 ⟶ 355:
continue;
int diff = p1 + p3 - 2 * p2;
int index =if (diff < 0 ? 0 : (diff > 0 ? 1 : -1);
if (index != -1) {strong_primes.add_prime(p2);
else if (diff > ++count2[index];0)
if weak_primes.add_prime(p2 < limit1);
++count1[index];
if (count2[index] <= max_print[index]) {
if (count2[index] > 1)
out[index] << ' ';
out[index] << p2;
}
}
p1 = p2;
p2 = p3;
}
strong_primes.print(std::cout, "strong");
for (int i = 0; i < 2; ++i) {
weak_primes.print(std::cout, "weak");
std::cout << "First " << max_print[i] << " " << strength[i] << " primes: " << out[i].str() << '\n';
std::cout << "Number of " << strength[i] << " primes below " << limit1 << ": " << count1[i] << '\n';
std::cout << "Number of " << strength[i] << " primes below " << limit2 << ": " << count2[i] << '\n';
}
return 0;
}</lang>
1,777

edits