-
Notifications
You must be signed in to change notification settings - Fork 7
/
650 Divisors of Binomial Product.pl
73 lines (51 loc) · 1.62 KB
/
650 Divisors of Binomial Product.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 15 January 2019
# https://github.com/trizen
# Formula for computing the sum of divisors of the product of binomials.
# Using the identities:
# Product_{k=0..n} binomial(n, k) = Product_{k=1..n} k^(2*k - n - 1)
# = hyperfactorial(n)/superfactorial(n)
# and the fact that the sigma function is multiplicative with:
# sigma_m(p^k) = (p^(m*(k+1)) - 1)/(p^m - 1)
# See also:
# https://oeis.org/A001142
# https://oeis.org/A323444
# Paper:
# Jeffrey C. Lagarias, Harsh Mehta
# Products of binomial coefficients and unreduced Farey fractions
# https://arxiv.org/abs/1409.4145
# https://projecteuler.net/problem=650
# WARNING: this program uses more than 3 GB of memory!
use 5.020;
use strict;
use warnings;
no warnings 'recursion';
use experimental qw(signatures);
use ntheory qw(powmod invmod mulmod forprimes todigits vecsum);
my @cache;
sub sum_of_digits ($n, $p) {
return 0 if ($n <= 0);
$cache[$n][$p] //= vecsum(todigits($n - 1, $p)) + sum_of_digits($n - 1, $p);
}
sub product_of_binomial_power ($n, $p) {
(2 * sum_of_digits($n, $p) - ($n - 1) * vecsum(todigits($n, $p))) / ($p - 1);
}
sub sigma_of_binomial_product ($n, $m = 1) {
my $prod = 1;
forprimes {
my $p = $_;
my $k = product_of_binomial_power($n, $p);
$prod = mulmod($prod, mulmod(powmod($p, $k + 1, $m) - 1, invmod($p - 1, $m), $m), $m);
} $n;
$prod;
}
my $n = 20_000;
my $sum = 0;
my $mod = 1000000007;
foreach my $k (1 .. $n) {
say "k = $k";
$sum += sigma_of_binomial_product($k, $mod);
$sum %= $mod;
}
say $sum;