-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.pl
199 lines (147 loc) · 4.95 KB
/
prog.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/perl
# Least non-palindromic number k such that k and its digital reversal both have exactly n prime divisors.
# https://oeis.org/A113548
# Known terms:
# 13, 12, 132, 1518, 15015, 204204, 10444434, 241879638, 20340535215, 242194868916
# a(n) >= A239696(n).
# This sequence does not allow ending in 0, else a(8) = 208888680, a(11) = 64635504163230 and a(13) = 477566276048801940. - Michael S. Branicky, Feb 14 2023
# New terms:
# a(11) = 136969856585562
# a(12) = 2400532020354468
# a(13) = 484576809394483806
# a(14) = 200939345091539746692
# Upper-bounds:
# a(13) <= 604973037030580218
# a(14) <= 202183806462387575826
# Lower-bounds:
# a(14) > 107173980829040893951
# Timings:
# a(11) is found in 5.7 seconds
# a(12) is found in 4.8 seconds
# a(13) is found in 2.5 minutes
# MPU::GMP prime_omega:
# a(11) in 19.0 seconds
# a(12) in 14.5 seconds
# Our method:
# a(11) in 9.7 seconds -- 7.8 seconds -- 6.5 seconds
# a(12) in 7.2 seconds -- 5.8 seconds -- 5.3 seconds
# While searching for a(14), it took 21 hours and 30 minutes to check the range [107173980829040893951, 214347961658081787902].
use 5.020;
use warnings;
use ntheory qw(:all);
use experimental qw(signatures);
use Math::GMPz;
use Math::Prime::Util::GMP;
sub mpz_is_omega_prime ($n, $k) {
state $z = Math::GMPz::Rmpz_init();
state $t = Math::GMPz::Rmpz_init();
Math::GMPz::Rmpz_set_str($z, $n, 10);
Math::GMPz::Rmpz_root($t, $z, $k);
my $trial_limit = Math::GMPz::Rmpz_get_ui($t);
if ($trial_limit > 1e3) {
$trial_limit = 1e3;
}
for (my $p = 2; $p <= $trial_limit; $p = next_prime($p)) {
if (Math::GMPz::Rmpz_divisible_ui_p($z, $p)) {
--$k;
Math::GMPz::Rmpz_set_ui($t, $p);
Math::GMPz::Rmpz_remove($z, $z, $t);
}
($k > 0) or last;
if (Math::GMPz::Rmpz_fits_ulong_p($z)) {
return is_omega_prime($k, Math::GMPz::Rmpz_get_ui($z));
}
}
if (Math::GMPz::Rmpz_cmp_ui($z, 1) == 0) {
return ($k == 0);
}
if ($k == 0) {
return (Math::GMPz::Rmpz_cmp_ui($z, 1) == 0);
}
if ($k == 1) {
if (Math::GMPz::Rmpz_fits_ulong_p($z)) {
return is_prime_power(Math::GMPz::Rmpz_get_ui($z));
}
return Math::Prime::Util::GMP::is_prime_power(Math::GMPz::Rmpz_get_str($z, 10));
}
Math::GMPz::Rmpz_ui_pow_ui($t, next_prime($trial_limit), $k);
if (Math::GMPz::Rmpz_cmp($z, $t) < 0) {
return 0;
}
Math::GMPz::Rmpz_fits_ulong_p($z)
? is_omega_prime($k, Math::GMPz::Rmpz_get_ui($z))
: (Math::Prime::Util::GMP::prime_omega(Math::GMPz::Rmpz_get_str($z, 10)) == $k);
}
foreach my $n (1..100) {
my $t = addint(urandomb($n), 1);
foreach my $k (1..20) {
if (is_omega_prime($k, $t)) {
mpz_is_omega_prime($t, $k) || die "error for: ($t, $k)";
}
elsif (mpz_is_omega_prime($t, $k)) {
die "counter-example: ($t, $k)";
}
}
}
sub generate($A, $B, $n) {
$A = vecmax($A, pn_primorial($n));
$A = Math::GMPz->new("$A");
my $u = Math::GMPz::Rmpz_init();
my @values = sub ($m, $lo, $j) {
Math::GMPz::Rmpz_tdiv_q($u, $B, $m);
Math::GMPz::Rmpz_root($u, $u, $j);
my $hi = Math::GMPz::Rmpz_get_ui($u);
if ($lo > $hi) {
return;
}
my @lst;
my $v = Math::GMPz::Rmpz_init();
foreach my $q (@{primes($lo, $hi)}) {
if ($q == 5 && Math::GMPz::Rmpz_even_p($m)) {
# Last digit can't be zero
next;
}
Math::GMPz::Rmpz_mul_ui($v, $m, $q);
while (Math::GMPz::Rmpz_cmp($v, $B) <= 0) {
if ($j == 1) {
if (Math::GMPz::Rmpz_cmp($v, $A) >= 0) {
my $s = Math::GMPz::Rmpz_get_str($v, 10);
my $r = reverse($s);
if ($r ne $s and (($r > ~0) ? mpz_is_omega_prime($r, $n) : is_omega_prime($n, $r))) {
my $w = Math::GMPz::Rmpz_init_set($v);
say("Found upper-bound: ", $w);
$B = $w if ($w < $B);
push @lst, $w;
}
}
}
else {
push @lst, __SUB__->($v, $q+1, $j-1);
}
Math::GMPz::Rmpz_mul_ui($v, $v, $q);
}
}
return @lst;
}->(Math::GMPz->new(1), 2, $n);
return sort { $a <=> $b } @values;
}
sub a($n) {
if ($n == 0) {
return 1;
}
#my $x = Math::GMPz->new(pn_primorial($n));
my $x = Math::GMPz->new("107173980829040893951");
my $y = 2*$x;
while (1) {
say("Sieving range: [$x, $y]");
my @v = generate($x, $y, $n);
if (scalar(@v) > 0) {
return $v[0];
}
$x = $y+1;
$y = 2*$x;
}
}
foreach my $n (14) {
say "a($n) = ", a($n);
}