Strong and weak primes: Difference between revisions
Content added Content deleted
(Refactored C++ code) |
|||
Line 311: | Line 311: | ||
#include "prime_sieve.hpp" |
#include "prime_sieve.hpp" |
||
⚫ | |||
⚫ | |||
const int limit2 = 10000000; |
|||
⚫ | |||
⚫ | |||
const int array_size = limit2 + 100; |
|||
class prime_info { |
|||
// find the prime numbers up to array_size |
|||
public: |
|||
⚫ | |||
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: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
}; |
|||
⚫ | |||
⚫ | |||
// write numbers with groups of digits separated according to the system default locale |
// write numbers with groups of digits separated according to the system default locale |
||
std::cout.imbue(std::locale("")); |
std::cout.imbue(std::locale("")); |
||
std::cout << std::fixed; |
|||
// count and print strong/weak prime numbers |
// count and print strong/weak prime numbers |
||
prime_info strong_primes(36); |
|||
⚫ | |||
prime_info weak_primes(37); |
|||
⚫ | |||
⚫ | |||
const char* strength[2] = { "strong", "weak" }; |
|||
int p1 = 2, p2 = 3; |
int p1 = 2, p2 = 3; |
||
for (int p3 = 5; p2 < limit2; ++p3) { |
for (int p3 = 5; p2 < limit2; ++p3) { |
||
Line 334: | Line 355: | ||
continue; |
continue; |
||
int diff = p1 + p3 - 2 * p2; |
int diff = p1 + p3 - 2 * p2; |
||
if (diff < 0) |
|||
strong_primes.add_prime(p2); |
|||
else if (diff > 0) |
|||
weak_primes.add_prime(p2); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
p1 = p2; |
p1 = p2; |
||
p2 = p3; |
p2 = p3; |
||
} |
} |
||
strong_primes.print(std::cout, "strong"); |
|||
for (int i = 0; i < 2; ++i) { |
|||
weak_primes.print(std::cout, "weak"); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return 0; |
return 0; |
||
}</lang> |
}</lang> |