Multi-base primes: Difference between revisions

Added Perl example
m (→‎{{header|REXX}}: shortened the RLE2DEC function, changed a comment.)
(Added Perl example)
Line 1,090:
 
real 0m20,566s</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
{{libheader|ntheory}}
<lang perl>use strict;
use warnings;
use feature 'say';
use List::AllUtils <firstidx max>;
use ntheory qw/fromdigits todigitstring primes/;
 
my(%prime_base, %max_bases, $l);
 
my $chars = 4;
my $upto = fromdigits( '1' . 'Z' x $chars, 36);
my @primes = @{primes( $upto )};
 
for my $base (2..36) {
my $n = todigitstring($base-1, $base) x $chars;
my $threshold = fromdigits($n, $base);
my $i = firstidx { $_ > $threshold } @primes;
map { push @{$prime_base{ todigitstring($primes[$_],$base) }}, $base } 0..$i-1;
}
 
$l = length and $max_bases{$l} = max( $#{$prime_base{$_}}, $max_bases{$l} // 0 ) for keys %prime_base;
 
for my $m (1 .. $chars) {
say "$m character strings that are prime in maximum bases: ", 1+$max_bases{$m};
for (sort grep { length($_) == $m } keys %prime_base) {
my @bases = @{($prime_base{$_})[0]};
say "$_: " . join ' ', @bases if $max_bases{$m} eq $#bases;
}
say ''
}</lang>
{{out}}
<pre>1 character strings that are prime in maximum 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 that are prime in maximum bases: 18
21: 3 5 6 8 9 11 14 15 18 20 21 23 26 29 30 33 35 36
 
3 character strings that are prime in maximum 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 that are prime in maximum 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 that are prime in maximum bases: 18
30271: 8 10 12 13 16 17 18 20 21 23 24 25 31 32 33 34 35 36</pre>
 
=={{header|Phix}}==
2,392

edits