-
Notifications
You must be signed in to change notification settings - Fork 33
/
sum_of_sigma.pl
executable file
·55 lines (42 loc) · 1.05 KB
/
sum_of_sigma.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 10 January 2018
# https://github.com/trizen
# Sum of the sigma(k) function, for 1 <= k <= n, where `sigma(k)` is `Sum_{d|k} d`.
# See also:
# https://oeis.org/A024916
# https://oeis.org/A072692
use 5.010;
use strict;
use warnings;
use Math::AnyNum qw(faulhaber_sum isqrt);
sub partial_sum_of_sigma { # O(sqrt(n)) complexity
my ($n) = @_;
my $s = isqrt($n);
my $u = int($n / ($s + 1));
my $sum = 0;
my $prev = faulhaber_sum($n, 1); # n-th triangular number
foreach my $k (1 .. $s) {
my $curr = faulhaber_sum(int($n/($k+1)), 1);
$sum += $k * ($prev - $curr);
$prev = $curr;
}
foreach my $k (1 .. $u) {
$sum += $k * int($n / $k);
}
return $sum;
}
foreach my $k (0 .. 10) {
say "a(10^$k) = ", partial_sum_of_sigma(10**$k);
}
__END__
a(10^0) = 1
a(10^1) = 87
a(10^2) = 8299
a(10^3) = 823081
a(10^4) = 82256014
a(10^5) = 8224740835
a(10^6) = 822468118437
a(10^7) = 82246711794796
a(10^8) = 8224670422194237
a(10^9) = 822467034112360628