-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog.pl
113 lines (86 loc) · 2.67 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
#!/usr/bin/perl
# Least non-palindromic number k such that k and its digital reversal both are the product of n distinct primes.
# Last digit can't be zero.
# Squarefree version of:
# https://oeis.org/A113548
# Known terms:
# 13, 15, 165, 1518, 15015, 246246, 10444434, 241879638, 20340535215, 458270430618, 136969856585562, 20446778282756826
use 5.020;
use ntheory qw(:all);
use experimental qw(signatures);
use Math::GMPz;
sub generate ($A, $B, $k, $callback) {
my $n = $k;
$A = vecmax($A, pn_primorial($k));
$A = Math::GMPz->new("$A");
my $u = Math::GMPz::Rmpz_init();
my $v = Math::GMPz::Rmpz_init();
sub ($m, $lo, $k) {
Math::GMPz::Rmpz_tdiv_q($u, $B, $m);
Math::GMPz::Rmpz_root($u, $u, $k);
my $hi = Math::GMPz::Rmpz_get_ui($u);
if ($lo > $hi) {
return;
}
if ($k == 1) {
Math::GMPz::Rmpz_cdiv_q($u, $A, $m);
if (Math::GMPz::Rmpz_fits_ulong_p($u)) {
$lo = vecmax($lo, Math::GMPz::Rmpz_get_ui($u));
}
elsif (Math::GMPz::Rmpz_cmp_ui($u, $lo) > 0) {
if (Math::GMPz::Rmpz_cmp_ui($u, $hi) > 0) {
return;
}
$lo = Math::GMPz::Rmpz_get_ui($u);
}
if ($lo > $hi) {
return;
}
forprimes {
Math::GMPz::Rmpz_mul_ui($v, $m, $_);
my $s = Math::GMPz::Rmpz_get_str($v, 10);
my $r = reverse($s);
if ($s ne $r and is_omega_prime($n, $r) and is_square_free($r)) {
my $t = Math::GMPz::Rmpz_init_set($v);
#say("Found upper-bound: ", $r);
$B = vecmin($t, $B);
$callback->($t);
}
} $lo, $hi;
return;
}
my $z = Math::GMPz::Rmpz_init();
foreach my $p (@{primes($lo, $hi)}) {
if ($p == 5 and Math::GMPz::Rmpz_even_p($m)) {
## last digit can't be zero
}
else {
Math::GMPz::Rmpz_mul_ui($z, $m, $p);
__SUB__->($z, $p+1, $k-1);
}
}
}->(Math::GMPz->new(1), 2, $k);
}
sub a($n) {
if ($n == 0) {
return 1;
}
my $x = Math::GMPz->new(pn_primorial($n));
my $y = 2*$x;
while (1) {
#say("Sieving range: [$x, $y]");
my @v;
generate($x, $y, $n, sub ($k) {
push @v, $k;
});
@v = sort { $a <=> $b } @v;
if (scalar(@v) > 0) {
return $v[0];
}
$x = $y+1;
$y = 2*$x;
}
}
foreach my $n (1..20) {
say "a($n) = ", a($n);
}