-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSonyGoForIt2.fsx
More file actions
41 lines (36 loc) · 1.22 KB
/
SonyGoForIt2.fsx
File metadata and controls
41 lines (36 loc) · 1.22 KB
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
open System
/// 問題を解く。
let solve x =
/// ガンマ関数の対数。
let loggamma x =
let N = 8.
let bernoullis = [ // ベルヌーイ数(B2から2刻み)
1.0 / 6.0
-1.0 / 30.0
1.0 / 42.0
-1.0 / 30.0
5.0 / 66.0
-691.0 / 2730.0
7.0 / 6.0
-3617.0 / 510.0
]
let rec increment v = function
| x when x < N -> increment (v * x) (x + 1.)
| x -> v, x
let v, x = increment 1. x
let w = 1. / (x * x)
List.zip bernoullis [2 .. 2 .. bernoullis.Length * 2] |> List.rev
|> Seq.map (fun (b, n) -> b / float (n * (n - 1)))
|> Seq.reduce (fun b1 b2 -> b1 * w + b2)
|> fun y -> y / x + 0.5 * log (2. * Math.PI) - log v - x + (x - 0.5) * log x
/// ガンマ関数。
let gamma x =
if x < 0. then Math.PI / (sin (Math.PI * x) * exp (loggamma(1. - x)))
else exp (loggamma x)
gamma (x + 1.) |> printfn "%g! = %.12g" x
printfn "i)"
[0 .. 10] |> List.iter (float >> solve)
printfn "ii)"
[0. .. 0.1 .. 10.] |> List.iter solve
printfn "iii)"
[-1.9 .. 0.1 .. -1.09] |> List.iter solve