fermat_pseudoprimes_from_multiple_mpz.pl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 08 March 2023
  4. # https://github.com/trizen
  5. # Generate Fermat pseudoprimes from a given multiple, to a given base.
  6. # See also:
  7. # https://trizenx.blogspot.com/2020/08/pseudoprimes-construction-methods-and.html
  8. use 5.036;
  9. use Math::GMPz;
  10. use ntheory qw(:all);
  11. sub fermat_pseudoprimes_from_multiple ($base, $m, $callback) {
  12. my $u = Math::GMPz::Rmpz_init();
  13. my $v = Math::GMPz::Rmpz_init();
  14. my $w = Math::GMPz::Rmpz_init_set_ui($base);
  15. my $L = znorder($base, $m);
  16. $m = Math::GMPz->new("$m");
  17. $L = Math::GMPz->new("$L");
  18. Math::GMPz::Rmpz_invert($v, $m, $L) || return;
  19. for (my $p = Math::GMPz::Rmpz_init_set($v) ; ; Math::GMPz::Rmpz_add($p, $p, $L)) {
  20. Math::GMPz::Rmpz_mul($v, $m, $p);
  21. Math::GMPz::Rmpz_sub_ui($u, $v, 1);
  22. Math::GMPz::Rmpz_powm($u, $w, $u, $v);
  23. if (Math::GMPz::Rmpz_cmp_ui($u, 1) == 0) {
  24. $callback->(Math::GMPz::Rmpz_init_set($v));
  25. }
  26. }
  27. }
  28. fermat_pseudoprimes_from_multiple(2, 341, sub ($n) { say $n });