prog_faster.pl 997 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/perl
  2. # Composite numbers (pseudoprimes) n, that are not Carmichael numbers, such that A000670(n) == 1 (mod n).
  3. # https://oeis.org/A289338
  4. # Known terms:
  5. # 169, 885, 2193, 8905, 22713
  6. use 5.010;
  7. use strict;
  8. use warnings;
  9. use ntheory qw(:all);
  10. use Math::GMPz;
  11. sub fubini_number {
  12. my ($n) = @_;
  13. state $F = [Math::GMPz::Rmpz_init_set_ui(1)];
  14. state $t = Math::GMPz::Rmpz_init_nobless();
  15. foreach my $i ($#{$F} + 1 .. $n) {
  16. my $w = Math::GMPz::Rmpz_init_set_ui(0);
  17. foreach my $k (0 .. $i - 1) {
  18. Math::GMPz::Rmpz_bin_uiui($t, $i, $i - $k);
  19. Math::GMPz::Rmpz_addmul($w, $F->[$k], $t);
  20. }
  21. $F->[$i] = $w;
  22. }
  23. $F->[$n];
  24. }
  25. my $t = Math::GMPz::Rmpz_init_nobless();
  26. foreach my $n (1..1e6) {
  27. next if is_prime($n);
  28. next if is_carmichael($n);
  29. if (Math::GMPz::Rmpz_mod_ui($t, fubini_number($n), $n) == 1) {
  30. say "Found term: $n";
  31. }
  32. }
  33. __END__
  34. Found term: 169
  35. Found term: 885
  36. Found term: 2193