Perl_program_for_A317126.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/perl
  2. use 5.014;
  3. use warnings;
  4. use HTTP::Tiny;
  5. use List::Util qw(all);
  6. use ntheory qw(is_prime is_carmichael);
  7. use Math::AnyNum qw(:overload is_div prod);
  8. # Download the b-file of A033502 added by Donovan Johnson
  9. my $response = HTTP::Tiny->new->get('http://oeis.org/A033502/b033502.txt');
  10. die "Failed to download the terms of A033502!\n" if not $response->{success};
  11. my $content = $response->{content};
  12. my @lines = split(/\n/, $content);
  13. my @A033502;
  14. foreach my $line (@lines) {
  15. $line =~ /\S/ or next;
  16. $line =~ /^#/ and next;
  17. my $n = Math::AnyNum->new((split(' ', $line))[1] // next);
  18. push @A033502, $n;
  19. }
  20. # Generate the factors of a Chernick number, given n
  21. # and k, where k is the number of distinct prime factors.
  22. sub chernick_carmichael_factors {
  23. my ($n, $k) = @_;
  24. (6 * $n + 1, 12 * $n + 1, (map { 2**$_ * 9 * $n + 1 } 1 .. $k - 2));
  25. }
  26. my @new_terms;
  27. my $limit = $A033502[-1];
  28. # Since each term in A033502 has 3 prime factors, we start with k=4 and generate all the
  29. # terms that are smaller than the largest term of A033502 and have at least k prime factors.
  30. for (my $k = 4 ; ; ++$k) {
  31. # We can stop the search when:
  32. # (6*m + 1) * (12*m + 1) * Product_{i=1..k-2} (9 * 2^i * m + 1)
  33. # is greater than the largest term of A033502, for m=1.
  34. last if prod(chernick_carmichael_factors(1, $k)) > $limit;
  35. # Generate the extended Chernick numbers with k distinct prime factors,
  36. # that are also Carmichael numbers, bellow the limit we're looking for.
  37. for (my $n = 1 ; ; ++$n) {
  38. my @f = chernick_carmichael_factors($n, $k);
  39. my $c = prod(@f);
  40. last if $c > $limit;
  41. next if not is_carmichael($c);
  42. # Check the conditions for an extended Chernick Carmichael number
  43. next if not all { is_prime($_) } @f;
  44. next if not is_div(($f[0] - 1) / 6, 2**($k - 4));
  45. push @new_terms, $c;
  46. }
  47. }
  48. # Sort the terms
  49. my @final_terms = sort { $a <=> $b } (@A033502, @new_terms);
  50. # Display the terms
  51. foreach my $k (0 .. $#final_terms) {
  52. say($k + 1, ' ', $final_terms[$k]);
  53. }