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