Jump to content

Multi-base primes: Difference between revisions

m
C++ performance improvement
m (C++ performance improvement)
m (C++ performance improvement)
Line 45:
 
=={{header|C++}}==
{{libheader|Primesieve}}
Originally translated from [[#Wren|Wren]] with ideas borrowed from other solutions.
The maximum base and number of characters can be specified as command line arguments.
Line 55 ⟶ 56:
#include <string>
#include <vector>
#include <primesieve.hpp>
 
class prime_sieve {
Line 67 ⟶ 69:
};
 
prime_sieve::prime_sieve(uint64_t limit) : sieve((limit + 1) / 2, truefalse) {
primesieve::iterator iter;
if (limit > 0)
uint64_t prime = iter.next_prime(); // consume 2
sieve[0] = false;
forwhile (uint64_t p(prime = 3;; piter.next_prime()) +<= 2limit) {
uint64_tsieve[prime q>> 1] = p * ptrue;
if (q >= limit)
break;
if (sieve[p >> 1]) {
uint64_t inc = 2 * p;
for (; q < limit; q += inc)
sieve[q >> 1] = false;
}
}
}
Line 180 ⟶ 175:
 
{{out}}
Maximum base 36 and maximum length 6. This takes 0.541 seconds to process up to 5 character strings and 2315 seconds to process up to 6 characters (3.2GHz Intel Core i5-4570).
<pre>
1-character strings which are prime in most bases: 34
Line 205 ⟶ 200:
</pre>
 
Maximum base 62 and maximum length 5. This takes 0.1615 seconds to process up to 4 character strings and 106.4 seconds to process up to 5 characters (3.2GHz Intel Core i5-4570).
<pre>
1-character strings which are prime in most bases: 60
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.