-
Notifications
You must be signed in to change notification settings - Fork 7
/
675 2 to omega of n.pl
63 lines (45 loc) · 1.4 KB
/
675 2 to omega of n.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 27 June 2019
# https://github.com/trizen
# Based on the identity:
# Sum_{d|n} 2^omega(d) = sigma_0(n^2)
# The algorithm iterates over each number k in 2..N,
# and computes sigma_0(k^2) = Prod_{p^e | k} (2*e + 1).
# By keeping track of the partial products, we find sigma_0(k!^2).
# Runtime: 44.576s
# https://projecteuler.net/problem=675
use 5.020;
use strict;
use warnings;
use ntheory qw(:all);
use experimental qw(signatures declared_refs);
sub F ($N, $mod) {
my @table;
my $total = 0;
my $partial = 1;
foreach my $k (2 .. $N) {
my $old = 1; # old product
foreach my \@pp (factor_exp($k)) {
my ($p, $e) = @pp;
$old = mulmod($old, 2 * $table[$p] + 1, $mod) if defined($table[$p]);
$table[$p] += $e;
$partial = mulmod($partial, 2 * $table[$p] + 1, $mod);
}
$partial = divmod($partial, $old, $mod); # remove the old product
$total += $partial;
$total %= $mod;
}
return $total;
}
my $MOD = 1000000087;
foreach my $n (1 .. 7) {
printf("F(10^%d) = %*s (mod %s)\n", $n, length($MOD) - 1, F(10**$n, $MOD), $MOD);
}
__END__
F(10^1) = 4821 (mod 1000000087)
F(10^2) = 930751395 (mod 1000000087)
F(10^3) = 822391759 (mod 1000000087)
F(10^4) = 979435692 (mod 1000000087)
F(10^5) = 476618093 (mod 1000000087)
F(10^6) = 420600586 (mod 1000000087)