Sieve of Eratosthenes: Difference between revisions
Content deleted Content added
added standard library alternative program for C++ |
m →Standard Library Patterns: comment clarity changes |
||
Line 1,299:
This implementation follows the standard library pattern of [http://en.cppreference.com/w/cpp/algorithm/iota std::iota]. The start and end iterators are provided for the container. The destination container is used for marking primes and then filled with the primes which are less than the container size. This method requires no memory allocation inside the function.
<lang cpp>#include <iostream>
#include <algorithm>
#include <vector>
Line 1,308 ⟶ 1,307:
{
if (start == end) return 0;
// clear the
std::fill(start, end, 0);
// mark composites with 1
Line 1,314 ⟶ 1,313:
{
if (*prime_it == 1) continue;
// determine the prime number
size_t stride = (prime_it - start) + 1;
// mark all multiples of this prime number up to max
ForwardIterator mark_it = prime_it;
while ((end - mark_it) > stride)
Line 1,323:
}
}
// copy marked primes into
ForwardIterator out_it = start;
for (ForwardIterator it = start + 1; it != end; ++it)
Line 1,345:
std::cout << std::endl;
return 1;
}</lang>▼
▲</lang>
=={{header|C sharp|C#}}==
|