-
Notifications
You must be signed in to change notification settings - Fork 33
/
rational_summation_of_fractions.pl
executable file
·89 lines (75 loc) · 1.6 KB
/
rational_summation_of_fractions.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
82
83
84
85
86
87
88
89
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 23 June 2016
# Website: https://github.com/trizen
# Rationalized summation of fractions, based on the identity:
#
# a c ad + bc
# --- + --- = ----------
# b d bd
# Combining this method with memoization, results in a practical
# generalized algorithm for summation of arbitrary fractions.
# In addition, with this method, any infinite sum can be converted into a limit.
# Example: ∞
# f(n) --- 1
# lim ---------- = \ ---- = e
# n->∞ _n_ / n!
# | | k! ---
# k=0 n=0
#
# where: _n_
# f(n+1) = (n+1)! * f(n) + | | k!
# k=0
# f(0) = 1
#
#====================================================
#
# Generally:
#
# x
# ---
# \ a(n) f(x)
# - ------ = ------
# / b(n) g(x)
# ---
# n=0
#
# where:
# | f(0) = a(0)
# | f(n) = b(n) * f(n-1) + a(n) * g(n-1)
#
# and:
# | g(0) = b(0)
# | g(n) = b(n) * g(n-1)
use 5.010;
use strict;
use warnings;
use Memoize qw(memoize);
use Math::AnyNum qw(:overload factorial);
memoize('b');
memoize('f');
memoize('g');
my $start = 0; # start iteration from this value
my $iter = 90; # number of iterations
sub a {
2**$_[0];
}
sub b {
factorial($_[0]);
}
sub f {
my ($n) = @_;
$n <= $start
? a($n)
: b($n) * f($n - 1) + a($n) * g($n - 1);
}
sub g {
my ($n) = @_;
$n <= $start
? b($n)
: b($n) * g($n - 1);
}
my $x = f($iter) / g($iter);
say $x;
say "e^2 =~ ", $x->as_dec(64);