-
Notifications
You must be signed in to change notification settings - Fork 0
/
smooth_generate.pl
146 lines (101 loc) · 2.53 KB
/
smooth_generate.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
#!/usr/bin/perl
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::GMPz;
use ntheory qw(:all);
sub check_valuation ($n, $p) {
#~ if ($p == 2) {
#~ return valuation($n, $p) < 5;
#~ }
#~ if ($p == 3) {
#~ return valuation($n, $p) < 3;
#~ }
#~ if ($p == 7) {
#~ return valuation($n, $p) < 3;
#~ }
($n % $p) != 0;
}
sub smooth_numbers ($limit, $primes) {
my @h = (1);
foreach my $p (@$primes) {
say "Prime: $p";
foreach my $n (@h) {
if ($n * $p <= $limit and check_valuation($n, $p)) {
push @h, $n * $p;
}
}
}
return \@h;
}
sub arithmetic_derivative {
my ($n) = @_;
my @factors = factor($n);
my $sum = 0;
foreach my $p (@factors) {
$sum += divint($n, $p);
}
return $sum;
}
sub dynamicPreimage ($N, $L) {
my %r = (1 => [1]);
foreach my $l (@$L) {
my %t;
foreach my $pair (@$l) {
my ($x, $y) = @$pair;
foreach my $d (divisors(divint($N, $x))) {
if (exists $r{$d}) {
push @{$t{mulint($x, $d)}}, map { mulint($_, $y) } @{$r{$d}};
}
}
}
while (my ($k, $v) = each %t) {
push @{$r{$k}}, @$v;
}
}
return if not exists $r{$N};
sort { $a <=> $b } @{$r{$N}};
}
sub cook_sigma ($N, $k) {
my %L;
foreach my $d (divisors($N)) {
next if ($d == 1);
foreach my $p (map { $_->[0] } factor_exp(subint($d, 1))) {
my $q = addint(mulint($d, subint(powint($p, $k), 1)), 1);
my $t = valuation($q, $p);
next if ($t <= $k or ($t % $k) or $q != powint($p, $t));
push @{$L{$p}}, [$d, powint($p, subint(divint($t, $k), 1))];
}
}
[values %L];
}
sub inverse_sigma ($N, $k = 1) {
dynamicPreimage($N, cook_sigma($N, $k));
}
sub isok ($n) {
my $d = arithmetic_derivative($n);
foreach my $k (inverse_sigma($n)) {
if ($d == $k) {
return $k;
}
}
return undef;
}
my @smooth_primes;
foreach my $p (@{primes(1000)}) {
if ($p == 2) {
push @smooth_primes, $p;
next;
}
if (is_smooth($p+1, 3)) {
push @smooth_primes, $p;
}
}
my $h = smooth_numbers(1e15, \@smooth_primes);
say "\nFound: ", scalar(@$h), " terms\n";
my %table;
foreach my $n (@$h) {
if (defined(my $k = isok($n))) {
say "Found: $k -> ", join(' * ', map { "$_->[0]^$_->[1]" } factor_exp($k));
}
}