largest_ndigit_carmichael.pl 707 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/perl
  2. # a(n) is the largest n-digit Carmichael number.
  3. # https://oeis.org/A063400
  4. use 5.020;
  5. use strict;
  6. use warnings;
  7. use Math::GMPz;
  8. use ntheory qw(:all);
  9. my %table;
  10. while (<>) {
  11. next if /^\h*#/;
  12. /\S/ or next;
  13. my $n = (split(' ', $_))[-1];
  14. $n || next;
  15. my $len = length($n);
  16. next if $len > 40;
  17. if ($n > ((~0) >> 1)) {
  18. $n = Math::GMPz->new("$n");
  19. }
  20. if (exists $table{$len}) {
  21. next if ($table{$len} > $n);
  22. }
  23. if (is_carmichael($n)) {
  24. $table{$len} //= $n;
  25. if ($n > $table{$len}) {
  26. $table{$len} = $n;
  27. }
  28. }
  29. }
  30. foreach my $len (sort { $a <=> $b } keys %table) {
  31. say "$len $table{$len}";
  32. }