let rec combinations acc size set = seq {
match size, set with
| n, x::xs ->
if n > 0 then yield! combinations (x::acc) (n - 1) xs
if n >= 0 then yield! combinations acc n xs
| 0, [] -> yield acc
| _, [] -> () }
let factorial n =
let rec loop i acc =
match i with
| 0 | 1 -> acc
| _ -> loop (i-1) (acc * i)
loop n 1
let combinations_count m n =
(factorial m) / (factorial (m-n))
您可以通过减少从 m-n+1 到 n 的数字乘积并使用 List 方法来改进一点:
let combinations_count_short m n =
[m-n+1..m] |> List.reduce (*)
这不是一件容易的事,前段时间我自己也遇到过。然后他们帮我找到了合适的解决方案,所以我就在这里复制它:
作者 : s952163
资源
用法:
为了更方便的使用,我建议你对函数进行包装
combinations按公式求解,不使用列表函数
您可以通过减少从 m-n+1 到 n 的数字乘积并使用 List 方法来改进一点: