-
Notifications
You must be signed in to change notification settings - Fork 34
/
modular_fibonacci_polynomial.pl
executable file
·57 lines (39 loc) · 1.27 KB
/
modular_fibonacci_polynomial.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 11 October 2017
# https://github.com/trizen
# Algorithm for computing a Fibonacci polynomial modulo m.
# (Sum_{k=1..n} (fibonacci(k) * x^k)) (mod m)
# See also:
# https://projecteuler.net/problem=435
use 5.020;
use strict;
use warnings;
use experimental qw(signatures lexical_subs);
use ntheory qw(lcm addmod mulmod factor_exp powmod);
sub pisano_period($mod) {
my sub find_period($mod) {
my ($x, $y) = (0, 1);
for (my $n = 1 ; ; ++$n) {
($x, $y) = ($y, addmod($x, $y, $mod));
if ($x == 0 and $y == 1) {
return $n;
}
}
}
my @prime_powers = map { $_->[0]**$_->[1] } factor_exp($mod);
my @power_periods = map { find_period($_) } @prime_powers;
return lcm(@power_periods);
}
sub modular_fibonacci_polynomial ($n, $x, $mod) {
$n %= pisano_period($mod);
my $sum = 0;
my ($f1, $f2) = (0, 1);
foreach my $k (1 .. $n) {
$sum = addmod($sum, mulmod($f2, powmod($x, $k, $mod), $mod), $mod);
($f1, $f2) = ($f2, addmod($f1, $f2, $mod));
}
return $sum;
}
say modular_fibonacci_polynomial(7, 11, 100000); #=> 57683
say modular_fibonacci_polynomial(10**15, 13, 6227020800); #=> 4631902275