Jump to content

Multi-base primes: Difference between revisions

C++ - extend to base 62
m (→‎{{header|REXX}}: changed some comments.)
(C++ - extend to base 62)
Line 46:
=={{header|C++}}==
{{trans|Wren}}
The maximum base and number of characters can be specified as command line arguments.
This takes 1.1 seconds to process up to 5 character strings and 40 seconds to process up to 6 characters (3.2GHz Intel Core i5).
<lang cpp>#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
 
Line 74 ⟶ 77:
}
 
template <typename T> void print(std::ostream& out, const std::vector<T>& v) {
void print(std::ostream& out, const std::vector<T>& v) {
if (!v.empty()) {
out << '[';
Line 87 ⟶ 89:
 
std::string to_string(const std::vector<unsigned int>& v) {
static constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string str;
for (auto i : v)
str += digits[i];
return str;
};
 
bool increment(std::vector<unsigned int>& digits, unsigned int max_base) {
class multi_base_primes {
auto i = digits.rbegin();
public:
for (; i != digits.rend() && *i + 1 == max_base; ++i)
explicit multi_base_primes(unsigned int depth);
void run() *i = 0;
if (i == digits.rend())
 
return false;
private:
++*i;
void process(const std::vector<unsigned int>& indices);
return true;
void nested_for(std::vector<unsigned int>& indices, unsigned int level);
}
static const unsigned int max_base = 36;
unsigned int max_depth;
std::vector<bool> sieve;
unsigned int most_bases = 0;
std::vector<std::pair<std::vector<unsigned int>, std::vector<unsigned int>>>
max_strings;
};
 
multi_base_primes::multi_base_primes(unsigned int depth)
: max_depth(depth),
sieve(prime_sieve(static_cast<uint64_t>(std::pow(max_base, depth)))) {}
 
void multi_base_primes::run(unsigned int max_base, unsigned int max_length) {
auto sieve =
for (unsigned int depth = 1; depth <= max_depth; ++depth) {
prime_sieve(static_cast<uint64_t>(std::pow(max_base, max_length)));
std::cout << depth
for (unsigned int length = 1; length <= max_length; ++length) {
<< " character strings which are prime in most bases: ";
max_strings.clear();std::cout << length
<< "-character strings which are prime in most bases: ";
most_bases = 0;
std::vector<unsigned int> indices(depth,most_bases = 0);
nested_for(indices, 0);std::vector<
std::pair<std::vector<unsigned int>, std::vector<unsigned int>>>
max_strings;
std::vector<unsigned int> digits(length, 0);
digits[0] = 1;
do {
auto max = std::max_element(digits.begin(), digits.end());
unsigned int min_base = 2;
if (max != digits.end())
min_base = std::max(min_base, *max + 1);
if (most_bases > max_base - min_base)
continue;
std::vector<unsigned int> bases;
for (unsigned int b = min_base; b <= max_base; ++b) {
uint64_t n = 0;
for (auto d : digits)
n = n * b + d;
if (sieve[n])
bases.push_back(b);
}
if (bases.size() > most_bases) {
most_bases = bases.size();
max_strings.clear();
}
if (bases.size() == most_bases)
max_strings.emplace_back(digits, bases);
} while (increment(digits, max_base));
std::cout << most_bases << '\n';
for (const auto& m : max_strings) {
Line 132 ⟶ 151:
}
 
int main(int argc, char** argv) {
void multi_base_primes::process(const std::vector<unsigned int>& indices) {
unsigned int max_base = 36;
auto max = std::max_element(indices.begin(), indices.end());
unsigned int min_basemax_length = 24;
iffor (maxint arg != indices.end()1; arg + 1 < argc; ++arg) {
min_baseif = std::max(min_basestrcmp(argv[arg], *max"-max_base") +== 10);
if (most_bases > max_base -= strtoul(argv[++arg], nullptr, min_base10);
else if (strcmp(argv[arg], "-max_length") == 0)
return;
max_length = strtoul(argv[++arg], nullptr, 10);
std::vector<unsigned int> bases;
for (unsigned int b = min_base; b <= max_base; ++b) {
uint64_t n = 0;
for (auto i : indices)
n = n * b + i;
if (sieve[n])
bases.push_back(b);
}
if (bases.size()max_base > most_bases62) {
std::cerr << "Max base cannot be greater than 62.\n";
most_bases = bases.size();
max_strings.clear()return EXIT_FAILURE;
}
if (bases.size()max_base ==< most_bases2) {
std::cerr << "Max base cannot be less than 2.\n";
max_strings.emplace_back(indices, bases);
return EXIT_FAILURE;
}
 
void multi_base_primes::nested_for(std::vector<unsigned int>& indices,
unsigned int level) {
if (level == indices.size()) {
process(indices);
} else {
indices[level] = (level == 0) ? 1 : 0;
while (indices[level] < max_base) {
nested_for(indices, level + 1);
++indices[level];
}
}
multi_base_primes(max_base, max_length);
}
return EXIT_SUCCESS;
 
int main() {
multi_base_primes mbp(6);
mbp.run();
}</lang>
 
{{out}}
Maximum base 36 and maximum length 6. This takes 1 second to process up to 5 character strings and 33 seconds to process up to 6 characters (3.2GHz Intel Core i5).
<pre>
1 -character strings which are prime in most bases: 34
2 -> [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
 
2 -character strings which are prime in most bases: 18
21 -> [3, 5, 6, 8, 9, 11, 14, 15, 18, 20, 21, 23, 26, 29, 30, 33, 35, 36]
 
3 -character strings which are prime in most bases: 18
131 -> [4, 5, 7, 8, 9, 10, 12, 14, 15, 18, 19, 20, 23, 25, 27, 29, 30, 34]
551 -> [6, 7, 11, 13, 14, 15, 16, 17, 19, 21, 22, 24, 25, 26, 30, 32, 35, 36]
737 -> [8, 9, 11, 12, 13, 15, 16, 17, 19, 22, 23, 24, 25, 26, 29, 30, 31, 36]
 
4 -character strings which are prime in most bases: 19
1727 -> [8, 9, 11, 12, 13, 15, 16, 17, 19, 20, 22, 23, 24, 26, 27, 29, 31, 33, 36]
5347 -> [8, 9, 10, 11, 12, 13, 16, 18, 19, 22, 24, 25, 26, 30, 31, 32, 33, 34, 36]
 
5 -character strings which are prime in most bases: 18
30271 -> [8, 10, 12, 13, 16, 17, 18, 20, 21, 23, 24, 25, 31, 32, 33, 34, 35, 36]
 
6 -character strings which are prime in most bases: 18
441431 -> [5, 8, 9, 11, 12, 14, 16, 17, 19, 21, 22, 23, 26, 28, 30, 31, 32, 33]
 
</pre>
 
Maximum base 62 and maximum length 5. This takes 0.5 seconds to process up to 4 character strings and 23 seconds to process up to 5 characters (3.2GHz Intel Core i5).
<pre>
1-character strings which are prime in most bases: 60
2 -> [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62]
 
2-character strings which are prime in most bases: 31
65 -> [7, 8, 9, 11, 13, 14, 16, 17, 18, 21, 22, 24, 27, 28, 29, 31, 32, 37, 38, 39, 41, 42, 43, 44, 46, 48, 51, 52, 57, 58, 59]
 
3-character strings which are prime in most bases: 33
1l1 -> [22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 51, 52, 53, 54, 57, 58, 59, 60, 61, 62]
b9b -> [13, 14, 15, 16, 17, 19, 20, 21, 23, 24, 26, 27, 28, 30, 31, 34, 36, 39, 40, 42, 45, 47, 49, 50, 52, 53, 54, 57, 58, 59, 60, 61, 62]
 
4-character strings which are prime in most bases: 32
1727 -> [8, 9, 11, 12, 13, 15, 16, 17, 19, 20, 22, 23, 24, 26, 27, 29, 31, 33, 36, 37, 38, 39, 41, 45, 46, 48, 50, 51, 57, 58, 60, 61]
417b -> [12, 13, 15, 16, 17, 18, 19, 21, 23, 25, 28, 30, 32, 34, 35, 37, 38, 39, 41, 45, 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 61, 62]
 
5-character strings which are prime in most bases: 30
50161 -> [7, 8, 9, 13, 17, 18, 19, 20, 25, 28, 29, 30, 31, 33, 35, 36, 38, 39, 41, 42, 43, 44, 47, 48, 52, 55, 56, 59, 60, 62]
 
</pre>
1,777

edits

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