BPSW_primality_test.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl
  2. # The Baillie-PSW primality test, named after Robert Baillie, Carl Pomerance, John Selfridge, and Samuel Wagstaff.
  3. # No counter-examples are known to this test.
  4. # Algorithm: given an odd integer n, that is not a perfect power:
  5. # 1. Perform a (strong) base-2 Fermat test.
  6. # 2. Find the first D in the sequence 5, −7, 9, −11, 13, −15, ... for which the Jacobi symbol (D/n) is −1.
  7. # Set P = 1 and Q = (1 − D) / 4.
  8. # 3. Perform a strong Lucas probable prime test on n using parameters D, P, and Q.
  9. # See also:
  10. # https://en.wikipedia.org/wiki/Lucas_pseudoprime
  11. # https://en.wikipedia.org/wiki/Baillie%E2%80%93PSW_primality_test
  12. use 5.020;
  13. use warnings;
  14. use experimental qw(signatures);
  15. use lib qw(../lib);
  16. use Math::AnyNum qw(is_prime is_power kronecker powmod as_bin bit_scan1);
  17. sub findQ($n) {
  18. # Find first D for which kronecker(D, n) == -1
  19. for (my $k = 2 ; ; ++$k) {
  20. my $D = (-1)**$k * (2 * $k + 1);
  21. if (kronecker($D, $n) == -1) {
  22. return ((1 - $D) / 4);
  23. }
  24. }
  25. }
  26. sub BPSW_primality_test($n) {
  27. return 0 if $n <= 1;
  28. return 1 if $n == 2;
  29. return 0 if !($n&1);
  30. return 0 if is_power($n);
  31. # Fermat base-2 test
  32. powmod(2, $n - 1, $n) == 1 or return 0;
  33. # Perform a strong Lucas probable prime test
  34. my $Q = findQ($n);
  35. my $d = $n + 1;
  36. my $s = bit_scan1($d, 0);
  37. my $t = $d >> ($s+1);
  38. my $U1 = 1;
  39. my ($V1, $V2) = (2, 1);
  40. my ($Q1, $Q2) = (1, 1);
  41. foreach my $bit (split(//, as_bin($t))) {
  42. $Q1 = ($Q1 * $Q2) % $n;
  43. if ($bit) {
  44. $Q2 = ($Q1 * $Q) % $n;
  45. $U1 = ($U1 * $V2) % $n;
  46. $V1 = ($V2 * $V1 - $Q1) % $n;
  47. $V2 = ($V2 * $V2 - 2*$Q2) % $n;
  48. }
  49. else {
  50. $Q2 = $Q1;
  51. $U1 = ($U1 * $V1 - $Q1) % $n;
  52. $V2 = ($V2 * $V1 - $Q1) % $n;
  53. $V1 = ($V1 * $V1 - 2*$Q2) % $n;
  54. }
  55. }
  56. $Q1 = ($Q1 * $Q2) % $n;
  57. $Q2 = ($Q1 * $Q) % $n;
  58. $U1 = ($U1 * $V1 - $Q1) % $n;
  59. $V1 = ($V2 * $V1 - $Q1) % $n;
  60. $Q1 = ($Q1 * $Q2) % $n;
  61. return 1 if $U1 == 0;
  62. return 1 if $V1 == 0;
  63. for (1 .. $s-1) {
  64. $V1 = ($V1 * $V1 - 2*$Q1) % $n;
  65. $Q1 = ($Q1 * $Q1) % $n;
  66. return 1 if $V1 == 0;
  67. }
  68. return 0;
  69. }
  70. #
  71. ## Run some tests
  72. #
  73. my $from = 1;
  74. my $to = 1e4;
  75. my $count = 0;
  76. foreach my $n ($from .. $to) {
  77. if (BPSW_primality_test($n)) {
  78. if (not is_prime($n)) {
  79. say "Counter-example: $n";
  80. }
  81. ++$count;
  82. }
  83. elsif (is_prime($n)) {
  84. say "Missed a prime: $n";
  85. }
  86. }
  87. say "There are $count primes between $from and $to.";