Ad
Mathematics
Algorithms
Logic
Numbers
Data Types

Combinations using big ints

#load "nums.cma";; open Num;;

let rec prod a b=
  let cmp=Num.compare_num a b in
  if cmp>0 then Num.num_of_int 0 else if cmp==0 then b else
  let b1=Num.sub_num b (Num.num_of_int 1) in
  Num.mult_num b (prod a b1);;

let num_comb n p=
  let nlp=Num.sub_num n p in
  let (p,nlp)=if (Num.compare_num p nlp)>0 then (nlp,p)else(p,nlp) in
  let one=(Num.num_of_int 1) in
  Num.div_num (
    prod (Num.add_num nlp one) n
  ) (prod one p);;
  
let comb (n:int) (p:int) :string=Num.string_of_num (num_comb (Num.num_of_int n) (Num.num_of_int p));;

comb 76 12 |> print_endline;;