-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path304 Primonacci.pl
More file actions
51 lines (37 loc) · 796 Bytes
/
304 Primonacci.pl
File metadata and controls
51 lines (37 loc) · 796 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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 20 August 2016
# License: GPLv3
# Website: https://github.com/trizen
# https://projecteuler.net/problem=304
# Runtime: 31.959s
use 5.010;
use strict;
use warnings;
use ntheory qw(next_prime);
use Memoize qw(memoize);
use Math::GMP qw(:constant);
memoize('a');
memoize('f');
my $mod = 1234567891011;
sub f {
my ($n) = @_;
return 1 if $n == 0;
return 1 if $n == 1;
my $k = int($n / 2);
($n % 2 == 0)
? (f($k) * f($k) + f($k - 1) * f($k - 1)) % $mod
: (f($k) * f($k + 1) + f($k - 1) * f($k)) % $mod;
}
sub a {
my ($n) = @_;
$n == 1
? next_prime(10**14)
: next_prime(a($n - 1));
}
my $sum = 0;
for my $i (1 .. 100_000) {
$sum += f(a($i) - 1);
$sum %= $mod;
}
say $sum;