-
Notifications
You must be signed in to change notification settings - Fork 0
/
upper-bounds.pl
112 lines (78 loc) · 2.81 KB
/
upper-bounds.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
#!/usr/bin/perl
# a(n) is the least prime p such that A001222(p+n) = A001222(p-n) = n.
# https://oeis.org/A333115
# Known terms:
# 23, 47, 1621, 373, 2352631, 9241, 18235603, 21968759, 27575049743, 2794997, 32503712890637, 304321037, 390917388671861, 277829661054961, 14392115869140641, 442395934703
# a(18) > 140737488355328
use 5.020;
use strict;
use warnings;
use ntheory qw(:all);
use experimental qw(signatures);
sub almost_prime_count_range ($n, $from, $upto) {
almost_prime_count($n, $upto) - almost_prime_count($n, $from-1);
}
sub divceil ($x,$y) { # ceil(x/y)
my $q = divint($x, $y);
(mulint($q, $y) == $x) ? $q : ($q+1);
}
sub almost_prime_numbers_in_range ($A, $B, $k, $callback) {
$A = vecmax($A, powint(2, $k));
sub ($m, $p, $k) {
if ($k == 1) {
forprimes {
$callback->(mulint($m, $_));
} vecmax($p, divceil($A, $m)), divint($B, $m);
return;
}
my $s = rootint(divint($B, $m), $k);
while ($p <= $s) {
my $t = mulint($m, $p);
# Optional optimization for tight ranges
if (divceil($A, $t) > divint($B, $t)) {
$p = next_prime($p);
next;
}
__SUB__->($t, $p, $k - 1);
$p = next_prime($p);
}
}->(1, 2, $k);
}
my $min_a18 = 140737488355328;
sub upper_bound($n, $from = 2, $upto = 2*$from) {
say "\n:: Searching an upper-bound for a($n)\n";
while (1) {
my $count = almost_prime_count_range($n, $from, $upto);
if ((($n == 18) ? ($upto > $min_a18) : 1) and $count > 0) {
say "Sieving range: [$from, $upto]";
say "This range contains: $count elements\n";
if (($n == 18) ? ($from < $min_a18) : 0) {
$from = $min_a18;
}
#~ foralmostprimes {
#~ my $v = $_;
#~ if (is_prime($v-$n) && is_almost_prime($n, $v - $n - $n)) {
#~ say "Found with v-n";
#~ die "a($n) <= ", $v-$n;
#~ }
#~ if (is_prime($v+$n) && is_almost_prime($n, $v + $n + $n)) {
#~ say "Found with v+n";
#~ die "a($n) <= ", ($v+$n);
#~ }
#~ } $n, $from, $upto;
almost_prime_numbers_in_range($from, $upto, $n, sub ($v) {
if (is_prime($v-$n) && is_almost_prime($n, $v - $n - $n)) {
say "Found with v-n";
die "a($n) <= ", $v-$n;
}
if (is_prime($v+$n) && is_almost_prime($n, $v + $n + $n)) {
say "Found with v+n";
die "a($n) <= ", ($v+$n);
}
})
}
$from = $upto+1;
$upto *= 2;
}
}
upper_bound(18);