BPSW_primality_test_mpz.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Math::GMPz;
  16. sub findQ ($n) {
  17. for (my $k = 2 ; ; ++$k) {
  18. my $D = (-1)**$k * (2 * $k + 1);
  19. if (Math::GMPz::Rmpz_si_kronecker($D, $n) == -1) {
  20. return ((1 - $D) / 4);
  21. }
  22. }
  23. }
  24. sub BPSW_primality_test ($n) {
  25. $n = Math::GMPz::Rmpz_init_set_str($n, 10) if ref($n) ne 'Math::GMPz';
  26. return 0 if Math::GMPz::Rmpz_cmp_ui($n, 1) <= 0;
  27. return 1 if Math::GMPz::Rmpz_cmp_ui($n, 2) == 0;
  28. return 0 if Math::GMPz::Rmpz_even_p($n);
  29. return 0 if Math::GMPz::Rmpz_perfect_power_p($n);
  30. state $d = Math::GMPz::Rmpz_init_nobless();
  31. state $t = Math::GMPz::Rmpz_init_nobless();
  32. Math::GMPz::Rmpz_set_ui($t, 2);
  33. # Fermat base-2 test (a strong Miller-Rabin test should be preferred instead)
  34. Math::GMPz::Rmpz_sub_ui($d, $n, 1);
  35. Math::GMPz::Rmpz_powm($t, $t, $d, $n);
  36. Math::GMPz::Rmpz_cmp_ui($t, 1) and return 0;
  37. my $P = 1;
  38. my $Q = findQ($n);
  39. Math::GMPz::Rmpz_add_ui($d, $d, 2); # d = n+1
  40. my $s = Math::GMPz::Rmpz_scan1($d, 0); # s = valuation(n, 2)
  41. Math::GMPz::Rmpz_div_2exp($t, $d, $s+1); # t = d >> (s+1)
  42. my $U1 = Math::GMPz::Rmpz_init_set_ui(1);
  43. my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set_ui($P));
  44. my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
  45. foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($t, 2))) {
  46. Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
  47. Math::GMPz::Rmpz_mod($Q1, $Q1, $n);
  48. if ($bit) {
  49. Math::GMPz::Rmpz_mul_si($Q2, $Q1, $Q);
  50. Math::GMPz::Rmpz_mul($U1, $U1, $V2);
  51. Math::GMPz::Rmpz_mul($V1, $V1, $V2);
  52. Math::GMPz::Rmpz_powm_ui($V2, $V2, 2, $n);
  53. Math::GMPz::Rmpz_sub($V1, $V1, $Q1);
  54. Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
  55. Math::GMPz::Rmpz_mod($V1, $V1, $n);
  56. Math::GMPz::Rmpz_mod($U1, $U1, $n);
  57. }
  58. else {
  59. Math::GMPz::Rmpz_set($Q2, $Q1);
  60. Math::GMPz::Rmpz_mul($U1, $U1, $V1);
  61. Math::GMPz::Rmpz_mul($V2, $V2, $V1);
  62. Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
  63. Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $n);
  64. Math::GMPz::Rmpz_sub($V2, $V2, $Q1);
  65. Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
  66. Math::GMPz::Rmpz_mod($V2, $V2, $n);
  67. Math::GMPz::Rmpz_mod($U1, $U1, $n);
  68. }
  69. }
  70. Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
  71. Math::GMPz::Rmpz_mul_si($Q2, $Q1, $Q);
  72. Math::GMPz::Rmpz_mul($U1, $U1, $V1);
  73. Math::GMPz::Rmpz_mul($V1, $V1, $V2);
  74. Math::GMPz::Rmpz_sub($U1, $U1, $Q1);
  75. Math::GMPz::Rmpz_sub($V1, $V1, $Q1);
  76. Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
  77. if (Math::GMPz::Rmpz_divisible_p($U1, $n)) {
  78. return 1;
  79. }
  80. if (Math::GMPz::Rmpz_divisible_p($V1, $n)) {
  81. return 1;
  82. }
  83. for (1 .. $s-1) {
  84. Math::GMPz::Rmpz_powm_ui($V1, $V1, 2, $n);
  85. Math::GMPz::Rmpz_submul_ui($V1, $Q1, 2);
  86. Math::GMPz::Rmpz_powm_ui($Q1, $Q1, 2, $n);
  87. if (Math::GMPz::Rmpz_divisible_p($V1, $n)) {
  88. return 1;
  89. }
  90. }
  91. return 0;
  92. }
  93. #
  94. ## Run some tests
  95. #
  96. use ntheory qw(is_prime);
  97. my $from = 1;
  98. my $to = 1e5;
  99. my $count = 0;
  100. foreach my $n ($from .. $to) {
  101. if (BPSW_primality_test($n)) {
  102. if (not is_prime($n)) {
  103. say "Counter-example: $n";
  104. }
  105. ++$count;
  106. }
  107. elsif (is_prime($n)) {
  108. say "Missed a prime: $n";
  109. }
  110. }
  111. say "There are $count primes between $from and $to.";