search.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/perl
  2. # Terms of A082180 that are not squares or cubes of primes.
  3. # https://oeis.org/A328497
  4. # Known terms:
  5. # 418, 27173, 2001341
  6. # a(4) > 10^8. - Giovanni Resta, Oct 18 2019
  7. # It's hard to find a(4), if it exists.
  8. # a(4) > 100000569
  9. # Upper-bound:
  10. # a(4) <= 16024189487
  11. # See also:
  12. # https://oeis.org/A228562
  13. use 5.010;
  14. use strict;
  15. use warnings;
  16. use integer;
  17. use experimental qw(signatures);
  18. use ntheory qw(:all);
  19. sub modular_binomial ($n, $k, $m) {
  20. my @rems_mods;
  21. foreach my $pair (factor_exp($m)) {
  22. my ($p, $e) = @$pair;
  23. push @rems_mods, [modular_binomial_prime_power($n, $k, $p, $e), powint($p,$e)];
  24. }
  25. return chinese(@rems_mods);
  26. }
  27. sub factorial_prime_pow ($n, $p) {
  28. ($n - vecsum(todigits($n, $p))) / ($p - 1);
  29. }
  30. sub binomial_prime_pow ($n, $k, $p) {
  31. #<<<
  32. factorial_prime_pow($n, $p)
  33. - factorial_prime_pow($k, $p)
  34. - factorial_prime_pow($n - $k, $p);
  35. #>>>
  36. }
  37. sub binomial_non_prime_part ($n, $k, $p, $e) {
  38. my $pe = powint($p, $e);
  39. my $r = $n - $k;
  40. my $acc = 1;
  41. my @fact_pe = (1);
  42. foreach my $x (1 .. $pe - 1) {
  43. if ($x % $p == 0) {
  44. $x = 1;
  45. }
  46. $acc = mulmod($acc, $x, $pe);
  47. push @fact_pe, $acc;
  48. }
  49. my $top = 1;
  50. my $bottom = 1;
  51. my $is_negative = 0;
  52. my $digits = 0;
  53. while ($n) {
  54. if ($acc != 1 and $digits >= $e) {
  55. $is_negative ^= $n & 1;
  56. $is_negative ^= $r & 1;
  57. $is_negative ^= $k & 1;
  58. }
  59. #<<<
  60. $top = mulmod($top, $fact_pe[$n % $pe], $pe);
  61. $bottom = mulmod($bottom, $fact_pe[$r % $pe], $pe);
  62. $bottom = mulmod($bottom, $fact_pe[$k % $pe], $pe);
  63. #>>>
  64. $n = $n / $p;
  65. $r = $r / $p;
  66. $k = $k / $p;
  67. ++$digits;
  68. }
  69. my $res = mulmod($top, invmod($bottom, $pe), $pe);
  70. if ($is_negative and ($p != 2 or $e < 3)) {
  71. $res = $pe - $res;
  72. }
  73. return $res;
  74. }
  75. sub modular_binomial_prime_power ($n, $k, $p, $e) {
  76. my $pow = binomial_prime_pow($n, $k, $p);
  77. if ($pow >= $e) {
  78. return 0;
  79. }
  80. my $modpow = $e - $pow;
  81. my $r = binomial_non_prime_part($n, $k, $p, $modpow) % powint($p,$modpow);
  82. if ($pow == 0) {
  83. return ($r % powint($p,$e));
  84. }
  85. return mulmod(powmod($p, $pow, powint($p,$e)), $r, powint($p,$e));
  86. }
  87. say modular_binomial(2*16024189487, 16024189487, 16024189487);
  88. my $count = 0;
  89. #forcomposites {
  90. forsquarefree {
  91. say "Testing: $_";
  92. my $k = $_;
  93. #~ if (is_square($k) and is_prime(sqrtint($k))) {
  94. #~ ## ok
  95. #~ }
  96. #~ elsif (is_power($k, 3) and is_prime(rootint($k, 3))) {
  97. #~ ## ok
  98. #~ }
  99. if (is_prime($k)) {
  100. ## ok
  101. }
  102. elsif (modular_binomial($k<<1, $k, $k) == 2) {
  103. die "Found: $k";
  104. }
  105. } 100000569, 2e8;