Java

  • Doppio fattoriale

    Il doppio fattoriale è definito come:

    \[ n!!= \left\{ \begin{matrix}1,\qquad\quad\ \mbox{se }n=0\mbox{ o }n=1; \\ n[(n-2)!!]\mbox{se }n\ge2.\qquad\qquad \end{matrix} \right. \]

    Per esempio

    \[8!!=8*6*4*2=384 \]

    Un algoritmo come questo può essere implementato molto facilmente in maniera ricorsiva, dove il metodo chiama più volte se stesso, infatti questa è l'implementazione che ne ho fatto:

    public static long doubleFact(long n){
    if(n>=52)return 0;
    if(n<=0)
    return 1;
    else
    return doubleFact(n-2)*n;

    }

     

  • Double Factorial

    The double factorial is a mathematical operation defined recursively as:
    \[n!!=\left\{\begin{matrix}1\ if\ n\leq 0\\n(n-2)!!\ otherwise \end{matrix} \right. \]
    eg.
    \[ \textit{8!!=8*6*4*2*1=  384  } \]
    An algorithm can be implemented very simply in a recursive way, where the method calls itself many times. That is an implementation:

    public static long doubleFact(long n){
    if(n>=52)return 0;
    if(n<=0)
    return 1;
    else
    return doubleFact(n-2)*n;

    }