upper-bounds.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 24 September 2022
  4. # https://github.com/trizen
  5. use 5.020;
  6. use warnings;
  7. use ntheory qw(:all);
  8. use experimental qw(signatures);
  9. use Math::GMPz;
  10. sub divceil ($x, $y) { # ceil(x/y)
  11. my $q = ($x / $y);
  12. ($q * $y == $x) ? $q : ($q + 1);
  13. }
  14. sub strong_fermat_psp_in_range ($A, $B, $k, $base, $primes, $callback) {
  15. $A = vecmax($A, pn_primorial($k));
  16. $A = Math::GMPz->new("$A");
  17. if ($A > $B) {
  18. return;
  19. }
  20. my $end = $#{$primes};
  21. my $k_exp = 1;
  22. my $congr = -1;
  23. sub ($m, $lambda, $j, $k) {
  24. my $y = rootint(($B / $m), $k);
  25. if ($k == 1) {
  26. my $x = divceil($A, $m);
  27. if ($primes->[-1] < $x) {
  28. return;
  29. }
  30. foreach my $i ($j .. $end) {
  31. my $p = $primes->[$i];
  32. last if ($p > $y);
  33. next if ($p < $x);
  34. my $t = $m * $p;
  35. if (($t - 1) % $lambda == 0 and ($t - 1) % znorder($base, $p) == 0) {
  36. my $valuation = valuation($p - 1, 2);
  37. if ($valuation > $k_exp and powmod($base, (($p - 1) >> $valuation) << $k_exp, $p) == ($congr % $p)) {
  38. $callback->($t);
  39. }
  40. }
  41. }
  42. return;
  43. }
  44. foreach my $i ($j .. $end) {
  45. my $p = $primes->[$i];
  46. last if ($p > $y);
  47. $base % $p == 0 and next;
  48. my $valuation = valuation($p - 1, 2);
  49. $valuation > $k_exp or next;
  50. powmod($base, (($p - 1) >> $valuation) << $k_exp, $p) == ($congr % $p) or next;
  51. my $L = lcm($lambda, znorder($base, $p));
  52. gcd($L, $m) == 1 or next;
  53. my $t = $m * $p;
  54. my $u = divceil($A, $t);
  55. my $v = ($B / $t);
  56. if ($u <= $v) {
  57. __SUB__->($t, $L, $i + 1, $k - 1);
  58. }
  59. }
  60. }
  61. ->(Math::GMPz->new(1), 1, 0, $k);
  62. }
  63. use IO::Handle;
  64. open my $fh, '>>', 'strong_fermat.txt';
  65. $fh->autoflush(1);
  66. my %upper_bounds = (
  67. 16 => Math::GMPz->new("431963846549014459308449974667236801"),
  68. 17 => Math::GMPz->new("1554352698725568399952746943189797571201"),
  69. 18 => Math::GMPz->new("2095080420396817592160909089382002325129301"),
  70. 19 => Math::GMPz->new("1085479319509324324097609405158976672897141701"),
  71. 20 => Math::GMPz->new("63948045755638594833121327281441883689072822750100"),
  72. );
  73. use List::Util qw(shuffle);
  74. #foreach my $lambda (80000 .. 1e6) {
  75. foreach my $lambda (shuffle 812700, 139230, 3197250, 4709250, 4709250, 2174130, 8824410, 20396250, 10442250, 982800, 7068600, 116953200, 88, 360, 3024, 12852, 8400, 39984, 18900, 486864, 529200) {
  76. #while (<>) {
  77. # chomp(my $lambda = $_);
  78. #$lambda >= 96600 or next;
  79. say "# Generating: $lambda";
  80. my @primes = grep { $_ > 2 and $lambda % $_ != 0 and $_ <= 3000 and is_prime($_) } map { $_ + 1 } divisors($lambda);
  81. foreach my $k (16..20) {
  82. #if (binomial(scalar(@primes), $k) < 1e6) {
  83. strong_fermat_psp_in_range(Math::GMPz->new(~0), $upper_bounds{$k}, $k, 2, \@primes, sub ($n) { say $n; say $fh $n; });
  84. #}
  85. }
  86. }