-
Notifications
You must be signed in to change notification settings - Fork 33
/
lucas_sequences_U_V_mpz.pl
executable file
·81 lines (66 loc) · 1.85 KB
/
lucas_sequences_U_V_mpz.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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::GMPz;
sub lucasUV ($n, $P, $Q) {
$n = Math::GMPz->new("$n");
$P = Math::GMPz->new("$P");
$Q = Math::GMPz->new("$Q");
my ($V1, $V2) = (Math::GMPz::Rmpz_init_set_ui(2), Math::GMPz::Rmpz_init_set($P));
my ($Q1, $Q2) = (Math::GMPz::Rmpz_init_set_ui(1), Math::GMPz::Rmpz_init_set_ui(1));
my $t = Math::GMPz::Rmpz_init();
my $v = Math::GMPz::Rmpz_init();
foreach my $bit (split(//, Math::GMPz::Rmpz_get_str($n, 2))) {
Math::GMPz::Rmpz_mul($Q1, $Q1, $Q2);
if ($bit) {
Math::GMPz::Rmpz_mul($Q2, $Q1, $Q);
Math::GMPz::Rmpz_mul($V1, $V1, $V2);
Math::GMPz::Rmpz_mul($t, $P, $Q1);
Math::GMPz::Rmpz_mul($V2, $V2, $V2);
Math::GMPz::Rmpz_sub($V1, $V1, $t);
Math::GMPz::Rmpz_submul_ui($V2, $Q2, 2);
}
else {
Math::GMPz::Rmpz_set($Q2, $Q1);
Math::GMPz::Rmpz_mul($V2, $V2, $V1);
Math::GMPz::Rmpz_mul($t, $P, $Q1);
Math::GMPz::Rmpz_mul($V1, $V1, $V1);
Math::GMPz::Rmpz_sub($V2, $V2, $t);
Math::GMPz::Rmpz_submul_ui($V1, $Q2, 2);
}
}
Math::GMPz::Rmpz_mul_2exp($t, $V2, 1);
Math::GMPz::Rmpz_submul($t, $P, $V1);
Math::GMPz::Rmpz_mul($v, $P, $P);
Math::GMPz::Rmpz_submul_ui($v, $Q, 4);
Math::GMPz::Rmpz_divexact($t, $t, $v);
return ($t, $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]