Last active
March 22, 2017 15:26
-
-
Save murasesyuka/a8427401479d95512d62954fa6df5292 to your computer and use it in GitHub Desktop.
Try F# Generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Microsoft.FSharp.Collections | |
open System.Collections.Generic | |
let rec fact (n:int64) :int64 = | |
if n = 0L then 1L | |
else n * fact (n-1L) | |
let comb (n:int64) (r:int64) = | |
let a = fact n | |
let b = fact r | |
let c = fact (n-r) | |
//printfn "f %d %d %d" a b c | |
(a / (b*c)) | |
let catalan_number (n:int64) :int64 = | |
match n with | |
| 0L -> 1L | |
| _ -> comb(2L*n) n - comb (2L*n) (n-1L) | |
[<EntryPoint>] | |
let main argv = | |
printfn "hello %A" argv | |
(* | |
printfn "fact %d" (fact 0) | |
printfn "fact %d" (fact 2) | |
printfn "fact %d" (fact 3) | |
printfn "fact %d" (fact 5) | |
//*) | |
(* | |
printfn "comb %b" (comb 2 0 = 1) | |
printfn "comb %b" (comb 2 1 = 2) | |
printfn "comb %b" (comb 2 2 = 1) | |
printfn "comb %b" (comb 3 0 = 1) | |
printfn "comb %b" (comb 3 1 = 3) | |
printfn "comb %b" (comb 3 2 = 3) | |
//*) | |
(* | |
let ans = [|1; 1; 2; 5; 14; 42; 132; 429; 1430; 4862; 16796; 58786; 208012; 742900; 2674440; 9694845|] | |
for index in 0..(ans.Length-1) do | |
let i = int64 (index) | |
printfn "catalan %d = %d ; %b" i (catalan_number i) (catalan_number i = int64(ans.[index])) | |
//*) | |
//(* | |
printfn "comb %d" (comb 20L 10L) | |
printfn "comb %d" (comb 20L 9L) | |
printfn "comb %d" (comb 22L 11L) | |
printfn "comb %d" (comb 22L 10L) | |
printfn "fact %d = %d" 20 (fact 20L) | |
printfn "fact %d = %d" 21 (fact 21L) | |
printfn "fact %d = %d" 22 (fact 22L) | |
//*) | |
(* | |
printfn "fact %d" (fact 12L) | |
printfn "fact %d" (fact 100L) | |
//printfn "fact %d" ((fact 14) / ((fact 7) * (fact 7)) ) | |
//printfn "fact %d" ((fact 14) / ((fact 7) * (fact 8)) ) | |
//*) | |
(* | |
hello [||] | |
comb 184756 | |
comb 167960 | |
comb -784 | |
comb -719 | |
fact 20 = 2432902008176640000 | |
fact 21 = -4249290049419214848 | |
fact 22 = -1250660718674968576 | |
続行するには何かキーを押してください . . . | |
*) | |
0 // 整数の終了コードします | |
; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment