-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path609 pi sequences.pl
More file actions
56 lines (40 loc) · 906 Bytes
/
609 pi sequences.pl
File metadata and controls
56 lines (40 loc) · 906 Bytes
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 14 September 2017
# https://github.com/trizen
# https://projecteuler.net/problem=609
# Runtime: ~4 minutes
use 5.010;
use strict;
use warnings;
use ntheory qw(primes);
sub p_609 {
my ($n, $mod) = @_;
my %primes;
@primes{@{primes($n)}} = ();
my @pc;
my $pc = 0;
my %table;
foreach my $i (1 .. $n) {
$pc++ if exists($primes{$i});
$pc[$i] = $pc;
my $u = $pc;
my $c = exists($primes{$i}) ? 0 : 1;
while ($u >= 1) {
++$c if !exists($primes{$u});
++$table{$c};
$u = $pc[$u];
}
}
my $prod = 1;
foreach my $key (keys %table) {
my $val = $table{$key};
if ($val > 0) {
$prod *= $val % $mod;
$prod %= $mod;
}
}
return $prod;
}
say p_609(1e8, 1000000007);