Erdös-Selfridge categorization of primes: Difference between revisions

Added Perl
m (→‎{{header|Phix}}: (s[i] &= x now p2js compatible))
(Added Perl)
Line 538:
Category 11 => first: 8524807, total: 1, last:8524807
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
{{libheader|ntheory}}
<lang perl>use strict;
use warnings;
use feature 'say';
use List::Util 'max';
use ntheory qw/factor/;
use Primesieve qw(generate_primes);
 
my @primes = (0, generate_primes (1, 10**8));
my %cat = (2 => 1, 3 => 1);
 
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
 
sub ES {
my ($n) = @_;
my @factors = factor $n + 1;
my $category = max map { defined $cat{$_} and $cat{$_} } @factors;
unless (defined $cat{ $factors[-1] }) {
$category = max $category, (1 + max map { $cat{$_} } factor 1 + $factors[-1]);
$cat{ $factors[-1] } = $category;
}
$category
}
 
my %es;
my $upto = 200;
push @{$es{ES($_)}}, $_ for @primes[1..$upto];
say "First $upto primes, Erdös-Selfridge categorized:";
say "$_: " . join ' ', sort {$a <=> $b} @{$es{$_}} for sort keys %es;
 
%es = ();
$upto = 1_000_000;
say "\nSummary of first @{[comma $upto]} primes, Erdös-Selfridge categorized:";
push @{$es{ES($_)}}, $_ for @primes[1..$upto];
printf "Category %2d: first: %9s last: %10s count: %s\n",
map { comma $_ } $_, (sort {$a <=> $b} @{$es{$_}})[0, -1], scalar @{$es{$_}}
for sort {$a <=> $b} keys %es;</lang>
{{out}}
<pre>First 200 primes, Erdös-Selfridge categorized:
1: 2 3 5 7 11 17 23 31 47 53 71 107 127 191 383 431 647 863 971 1151
2: 13 19 29 41 43 59 61 67 79 83 89 97 101 109 131 137 139 149 167 179 197 199 211 223 229 239 241 251 263 269 271 281 283 293 307 317 349 359 367 373 419 433 439 449 461 479 499 503 509 557 563 577 587 593 599 619 641 643 659 709 719 743 751 761 769 809 827 839 881 919 929 953 967 991 1019 1033 1049 1069 1087 1103 1187 1223
3: 37 103 113 151 157 163 173 181 193 227 233 257 277 311 331 337 347 353 379 389 397 401 409 421 457 463 467 487 491 521 523 541 547 569 571 601 607 613 631 653 683 701 727 733 773 787 797 811 821 829 853 857 859 877 883 911 937 947 983 997 1009 1013 1031 1039 1051 1061 1063 1091 1097 1117 1123 1153 1163 1171 1181 1193 1217
4: 73 313 443 617 661 673 677 691 739 757 823 887 907 941 977 1093 1109 1129 1201 1213
5: 1021
 
Summary of first 1,000,000 primes, Erdös-Selfridge categorized:
Category 1: first: 2 last: 10,616,831 count: 46
Category 2: first: 13 last: 15,482,669 count: 10,497
Category 3: first: 37 last: 15,485,863 count: 201,987
Category 4: first: 73 last: 15,485,849 count: 413,891
Category 5: first: 1,021 last: 15,485,837 count: 263,109
Category 6: first: 2,917 last: 15,485,857 count: 87,560
Category 7: first: 15,013 last: 15,484,631 count: 19,389
Category 8: first: 49,681 last: 15,485,621 count: 3,129
Category 9: first: 532,801 last: 15,472,811 count: 363
Category 10: first: 1,065,601 last: 15,472,321 count: 28
Category 11: first: 8,524,807 last: 8,524,807 count: 1</pre>
 
=={{header|Phix}}==
2,392

edits