-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsecant_numbers.pl
More file actions
executable file
·42 lines (31 loc) · 901 Bytes
/
secant_numbers.pl
File metadata and controls
executable file
·42 lines (31 loc) · 901 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
#!/usr/bin/perl
# Algorithm for computing the secant numbers (also known as Euler numbers):
#
# 1, 1, 5, 61, 1385, 50521, 2702765, 199360981, 19391512145, 2404879675441, 370371188237525, ...
#
# Algorithm presented in the book:
#
# Modern Computer Arithmetic
# - by Richard P. Brent and Paul Zimmermann
#
# See also:
# https://oeis.org/A000364
# https://en.wikipedia.org/wiki/Euler_number
use 5.010;
use strict;
use warnings;
use Math::GMPz;
sub secant_numbers {
my ($n) = @_;
my @S = (Math::GMPz::Rmpz_init_set_ui(1));
foreach my $k (1 .. $n) {
Math::GMPz::Rmpz_mul_ui($S[$k] = Math::GMPz::Rmpz_init(), $S[$k - 1], $k);
}
foreach my $k (1 .. $n) {
foreach my $j ($k + 1 .. $n) {
Math::GMPz::Rmpz_addmul_ui($S[$j], $S[$j - 1], ($j - $k + 2) * ($j - $k));
}
}
return @S;
}
say join(', ', secant_numbers(10));