-
Notifications
You must be signed in to change notification settings - Fork 33
/
pi_from_infinity.pl
executable file
·81 lines (63 loc) · 1.55 KB
/
pi_from_infinity.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
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 15 May 2016
# Website: https://github.com/trizen
# Generic implementations for infinite sums, infinite
# products, continued fractions and nested radicals.
use 5.020;
use warnings;
no warnings 'recursion';
use experimental qw(signatures);
#
## Infinite sum
#
sub sum ($from, $to, $expr) {
my $sum = 0;
for my $i ($from .. $to) {
$sum += $expr->($i);
}
$sum;
}
say "=> PI from an infinite sum:";
say 4 * sum(0, 100000, sub($n) { (-1)**$n / (2 * $n + 1) });
#
## Infinite product
#
sub prod ($from, $to, $expr) {
my $prod = 1;
for my $i ($from .. $to) {
$prod *= $expr->($i);
}
$prod;
}
say "=> PI from an infinite product:";
say 2 / prod(1, 100000, sub($n) { 1 - 1 / (4 * $n**2) });
#
## Continued fractions
#
sub cfrac ($from, $to, $num, $den) {
return 0 if ($from > $to);
$num->($from) / ($den->($from) + cfrac($from + 1, $to, $num, $den));
}
say "=> PI from a continued fraction:";
say 4 / (1 + cfrac(1, 100000, sub($n) { $n**2 }, sub($n) { 2 * $n + 1 }));
#
## Nested radicals
#
sub nestrad ($from, $to, $coeff, $expr) {
return 0 if ($from > $to);
$expr->($coeff->($from) + nestrad($from + 1, $to, $coeff, $expr));
}
say "=> PI from nested square roots:";
say 2 / prod(
1, 100,
sub ($n) {
nestrad(1, $n, sub($) { 2 }, sub($x) { sqrt($x) }) / 2;
}
);
# A formula by N. J. Wildberger
# https://www.youtube.com/watch?v=lcIbCZR0HbU
say sqrt(4**(12+1) *
(2 - nestrad(1, 12, sub($) { 2 }, sub($x) { sqrt($x) }))
);