title: getting overflow in bash script to compute the multinomial coeffcients
body: <p>I'm using bash to compute the multinomial coefficients. The code follows bellow:</p>
  
  <pre><code>#!/bin/bash
  
  function factorial {
    declare n=$1
      (( n &lt; 2 )) &amp;&amp; echo 1 &amp;&amp; return
        echo $(( n * $(factorial $((n-1))) ))
  }
  
  function binomial {
    declare n=$1
    declare k=$2
    echo $(( $(factorial $((n))) / ( $(factorial $((k))) * $(factorial $((n-k)))  ) ))
  }
  
  function multinomial {
    arr=("$@")
    declare mcoeff=1
    declare n=0
    for k in "${arr[@]}";
    do
      ((n=$n+$k))
      ((mcoeff=$mcoeff*$(binomial "$n" "$k")))
    done
    echo "$mcoeff"
  }
  
  multinomial $@
  </code></pre>
  
  <p>It seems I have an overflow in some situations.</p>
  
  <pre><code>$ ./multinomial.sh 4 5 6
  630630
  $ ./multinomial.sh 4 5 6 7
  -119189070
  </code></pre>
  
  <p>Any idea how to fix this?</p>