/
ch-2.raku
53 lines (44 loc) · 1.17 KB
/
ch-2.raku
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
#!/usr/bin/env raku
# :vim ft=raku sw=4 expandtab
use v6.d;
use lib $?FILE.IO.parent(2).add("lib");
use Test;
=begin comment
Task 2: Gamma Function
Implement subroutine gamma() using the Lanczos approximation method.
Example
print gamma(3); # 1.99
print gamma(5); # 24
print gamma(7); # 719.99
=end comment
# NOTE: No credit to me. I can grab Rosetta Code which I don't
# understand and and test it slightly.
sub Γ( \z ) {
constant g = 9;
z < .5 ?? pi/ sin(pi * z) / Γ(1 - z) !!
sqrt(2*pi) *
(z + g - 1/2)**(z - 1/2) *
exp(-(z + g - 1/2)) *
[+] <
1.000000000000000174663
5716.400188274341379136
-14815.30426768413909044
14291.49277657478554025
-6348.160217641458813289
1301.608286058321874105
-108.1767053514369634679
2.605696505611755827729
-0.7423452510201416151527e-2
0.5384136432509564062961e-7
-0.4023533141268236372067e-8
> Z* 1, |map 1/(z + *), 0..*
}
plan 20;
my constant @factorial = gather for 0 .. ∞ {
once { take 1; next };
take [×] 1 .. $_-1
}
for 1 .. 20 {
is-approx Γ($_), @factorial[$_], "Γ($_) approximates " ~ @factorial[$_];
}
done-testing;