Truncatable primes: Difference between revisions
m
Put Sieve of Eratosthenes implementation in separate header file
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (Put Sieve of Eratosthenes implementation in separate header file) |
||
Line 529:
=={{header|C++}}==
<lang cpp>#include <iostream>
#include
int main()
Line 536:
// find the prime numbers up to the limit
isprime[0] = isprime[1] = false;▼
for (int p = 2; p * p <= limit; ++p)▼
{▼
if (isprime[p])▼
{▼
for (int i = p * p; i <= limit; i += p)▼
isprime[i] = false;▼
}▼
}▼
int largest_left = 0;
int largest_right = 0;
Line 551 ⟶ 543:
for (int p = limit; p >= 2; --p)
{
if (!
continue;
bool left_truncatable = true;
for (int n = 10, q = p; p > n; n *= 10)
{
if (!
{
left_truncatable = false;
Line 572 ⟶ 564:
for (int p = limit; p >= 2; --p)
{
if (!
continue;
bool right_truncatable = true;
for (int q = p/10; q > 0; q /= 10)
{
if (!
{
right_truncatable = false;
Line 594 ⟶ 586:
return 0;
}</lang>
Contents of sieve_of_eratosthenes.h:
<lang cpp>#ifndef SIEVE_OF_ERATOSTHENES_H
#define SIEVE_OF_ERATOSTHENES_H
#include <vector>
class sieve_of_eratosthenes
{
public:
explicit sieve_of_eratosthenes(size_t);
bool is_prime(size_t) const;
private:
std::vector<bool> is_prime_;
};
inline bool sieve_of_eratosthenes::is_prime(size_t n) const
{
return is_prime_[n];
}
inline sieve_of_eratosthenes::sieve_of_eratosthenes(size_t max)
: is_prime_(max, true)
{
▲ {
▲ {
▲ }
▲ }
}
#endif</lang>
{{out}}
|