Strong and weak primes: Difference between revisions
Refactored C++ code
(Refactored C++ code) |
|||
Line 311:
#include "prime_sieve.hpp"
int main() {▼
▲ const int limit2 = 10000000;
const int max_print[2] = { 36, 37 };▼
class prime_info {
public:
prime_sieve sieve(array_size);▼
explicit prime_info(int max) : max_print(max) {}
void add_prime(int prime) {
if (prime < limit1)
}▼
}▼
void print(std::ostream& os, const char* name) const {
}
private:
};
▲int main() {
// write numbers with groups of digits separated according to the system default locale
std::cout.imbue(std::locale(""));
// 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];
int p1 = 2, p2 = 3;
for (int p3 = 5; p2 < limit2; ++p3) {
Line 334 ⟶ 355:
continue;
int diff = p1 + p3 - 2 * p2;
else if (diff >
▲ ++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");
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>
|