-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_factors_2.pl
164 lines (120 loc) · 4 KB
/
report_factors_2.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 03 May 2022
# https://github.com/trizen
# Report factorizations to factordb.
# The factorizations must be in the following format:
# n = p1 * p2 * ...
use 5.020;
use autodie;
use warnings;
use WWW::Mechanize;
use List::Util qw(all);
use Math::AnyNum qw(:overload :all);
use experimental qw(signatures);
use Getopt::Long qw(GetOptions);
use constant {
USE_TOR_PROXY => 1, # true to use the Tor proxy to connect to factorDB (127.0.0.1:9050)
COMPRESS_FACTORS => 1, # compress small factors into slightly larger composite factors
COMPRESSION_DIGITS => 45, # the compressed composite factors will have <= this many digits
};
my $mech = WWW::Mechanize->new(
autocheck => 1,
show_progress => 1,
stack_depth => 10,
timeout => 600,
agent => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
);
{
state $accepted_encodings = HTTP::Message::decodable();
$mech->default_header('Accept-Encoding' => $accepted_encodings);
};
{
require LWP::ConnCache;
my $cache = LWP::ConnCache->new;
$cache->total_capacity(undef); # no limit
$mech->conn_cache($cache);
};
if (USE_TOR_PROXY) {
$mech->proxy(['http', 'https'], "socks://127.0.0.1:9050");
}
my $from_slice = 0;
my $slice_len = 500;
GetOptions('s|slice=i' => \$from_slice,
'l|length=i' => \$slice_len,)
or die("Error in command line arguments\n");
say ":: Collecting factors...";
my %factors;
while (<>) {
my @f;
my $value;
if (/^(\d+)\s*=\s*(.+)/) {
my ($n, $factors) = ($1, $2);
$n = Math::AnyNum->new($n);
$n > 1 or next;
$value = "$n";
my %seen;
@f = grep { $_ > 1e4 } map { Math::AnyNum->new($_) } grep { !$seen{$_}++ } split(/\s*\*\s*/, $factors);
(all { is_div($n, $_) } @f)
or do {
warn "error in factors (@f) for n = $n\n";
@f = grep { $_ > 1 } map { gcd($n, $_) } @f;
};
}
elsif (/^(.+?)\s*=\s*(.+)/) { # the number is an expression
my ($expr, $factors) = ($1, $2);
$value = $expr;
my %seen;
@f = grep { $_ > 1e4 } map { Math::AnyNum->new($_) } grep { !$seen{$_}++ } split(/\s*\*\s*/, $factors);
}
else {
warn "[WARN] Invalid line: $_";
next;
}
@f || next;
if (COMPRESS_FACTORS) {
my @compressed;
while (@f) {
my $prod = shift(@f);
while (@f and length(lcm($prod, $f[0])) <= COMPRESSION_DIGITS) {
$prod = lcm($prod, shift(@f));
}
push @compressed, $prod;
}
@f = @compressed;
}
push @{$factors{$value}}, @f;
}
my @list;
foreach my $n (sort keys %factors) {
my %seen;
push @list, ("$n = " . join(" * ", grep { !$seen{$_}++ } @{$factors{$n}}));
}
say ":: Reporting factors...";
my $count = 0;
my $total_count = sprintf('%.0f', 0.5 + scalar(@list) / $slice_len);
if ($from_slice > 0) {
say ":: Starting from slice number $from_slice...";
}
my $url = "http://factordb.com/search.php";
while (@list) {
++$count;
my @slice = splice(@list, 0, $slice_len);
my $factor_table = join("\n", @slice);
next if ($count < $from_slice);
say "\n:: Sending slice $count of $total_count with ", scalar(@slice), " entries...";
$mech->get($url);
my $resp = $mech->submit_form(
form_number => 1,
fields => {
'msub' => $factor_table,
}
);
if ($resp->is_success) {
say ":: Success!";
sleep 1;
}
else {
die ":: There was an error...\n";
}
}