generate.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/perl
  2. # Lexicographically earliest strictly increasing sequence of prime numbers whose partial products, starting from the second, are all Fermat pseudoprimes to base 2 (A001567).
  3. # https://oeis.org/A374029
  4. # Known terms:
  5. # 11, 31, 41, 61, 181, 54001, 54721, 61561, 123121, 225721, 246241, 430921, 523261, 800281, 2400841, 9603361, 28810081, 76826881, 96033601, 15909022209601, 133133396385601, 5791302742773601, 15443473980729601, 61773895922918401, 386086849518240001, 13706083157897520001, 1398406568955065280001, 44872171911558407520001
  6. use 5.036;
  7. use ntheory qw(:all);
  8. use Math::Prime::Util::GMP;
  9. use Math::GMPz;
  10. # PARI/GP program:
  11. # my(P=List(11),base=2); print1(P[1], ", "); while(1, my(m = vecprod(Vec(P))); my(L = znorder(Mod(base, m))); forstep(p=lift(1/Mod(m, L)), oo, L, if(p > P[#P] && isprime(p) && base % p != 0, if((m*p-1) % znorder(Mod(base, p)) == 0, print1(p, ", "); listput(P, p); break)))); \\ ~~~~
  12. # PARI/GP program (faster):
  13. # my(P=List(11),base=2); my(m = vecprod(Vec(P))); my(L = znorder(Mod(base, m))); print1(P[1], ", "); while(1, forstep(p=lift(1/Mod(m, L)), oo, L, if(p > P[#P] && isprime(p) && base % p != 0, if((m*p-1) % znorder(Mod(base, p)) == 0, print1(p, ", "); listput(P, p); L = lcm(L, znorder(Mod(base, p))); m *= p; break)))); \\ ~~~~
  14. my @primes = (11);
  15. my $base = 2;
  16. my $m = Math::GMPz->new(vecprod(@primes));
  17. my $L = Math::GMPz->new(znorder($base, $m));
  18. while (1) {
  19. my $u = Math::GMPz::Rmpz_init();
  20. my $v = Math::GMPz::Rmpz_init();
  21. my $t = Math::GMPz::Rmpz_init();
  22. Math::GMPz::Rmpz_invert($v, $m, $L);
  23. my $prev_p = $primes[-1];
  24. for (my $p = $v ; ; Math::GMPz::Rmpz_add($p, $p, $L)) {
  25. if ($p > $prev_p and Math::GMPz::Rmpz_probab_prime_p($p, 10)) {
  26. Math::GMPz::Rmpz_mul($u, $m, $p);
  27. Math::GMPz::Rmpz_sub_ui($u, $u, 1);
  28. Math::GMPz::Rmpz_set_str($t, Math::Prime::Util::GMP::znorder($base, $p), 10);
  29. if (Math::GMPz::Rmpz_divisible_p($u, $t)) {
  30. say $p;
  31. push @primes, Math::GMPz::Rmpz_init_set($p);
  32. $L = Math::GMPz->new(lcm($L, $t));
  33. $m *= $p;
  34. last;
  35. }
  36. }
  37. }
  38. }
  39. __END__
  40. 15909022209601
  41. 133133396385601
  42. 5791302742773601
  43. 15443473980729601
  44. 61773895922918401
  45. 386086849518240001
  46. 13706083157897520001
  47. 1398406568955065280001
  48. 44872171911558407520001