random_miller-rabin_pseudoprimes.pl 573 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/perl
  2. # Generate random probable Miller-Rabin pseudoprimes of the form:
  3. #
  4. # `n = p * (2*p - 1)`
  5. #
  6. # where `2*p - 1` is also prime.
  7. use 5.010;
  8. use strict;
  9. use warnings;
  10. use Math::GMPz;
  11. use ntheory qw(:all);
  12. my @bases = (2, 3, 5); # Miller-Rabin pseudoprimes to these bases
  13. my $bits = 50; # bits of p
  14. foreach my $n (1 .. 1e6) {
  15. my $p = Math::GMPz->new(random_nbit_prime($bits));
  16. if (is_prob_prime(2 * $p - 1)) {
  17. my $n = $p * ($p * 2 - 1);
  18. if (is_strong_pseudoprime($n, @bases)) {
  19. say $n;
  20. }
  21. }
  22. }