-
Notifications
You must be signed in to change notification settings - Fork 33
/
lucas_sequences_U_V.pl
executable file
·66 lines (52 loc) · 1.05 KB
/
lucas_sequences_U_V.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
#!/usr/bin/perl
# Algorithm due to Aleksey Koval for computing the Lucas U and V sequences.
# See also:
# https://en.wikipedia.org/wiki/Lucas_sequence
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::AnyNum qw(:overload digits);
sub lucasUV ($n, $P, $Q) {
my ($V1, $V2) = (2, $P);
my ($Q1, $Q2) = (1, 1);
my @bits = digits($n, 2);
while (@bits) {
$Q1 *= $Q2;
if (pop @bits) {
$Q2 = ($Q1 * $Q);
$V1 = ($V2 * $V1 - $P * $Q1);
$V2 = ($V2 * $V2 - 2 * $Q2);
}
else {
$Q2 = $Q1;
$V2 = ($V2 * $V1 - $P * $Q1);
$V1 = ($V1 * $V1 - 2 * $Q2);
}
}
my $Uk = (2 * $V2 - $P * $V1) / ($P * $P - 4 * $Q);
return ($Uk, $V1);
}
foreach my $n (1 .. 20) {
say "[", join(', ', lucasUV($n, 1, -1)), "]";
}
__END__
[1, 1]
[1, 3]
[2, 4]
[3, 7]
[5, 11]
[8, 18]
[13, 29]
[21, 47]
[34, 76]
[55, 123]
[89, 199]
[144, 322]
[233, 521]
[377, 843]
[610, 1364]
[987, 2207]
[1597, 3571]
[2584, 5778]
[4181, 9349]
[6765, 15127]