The Double Factorial – Solution

This month’s Exercise is to write code to calculate a double factorial, which uses the !! notation. A double factorial works like a factorial, but uses only odd or even values based on the parity of the starting value.

I wrote my first solution as a loop. The loop tallies the factors of the starting value minus two for each spin of the loop. It repeats as long as the original value, f, is greater than 2:

while(f>2)
    t *= (f-=2);

The double factorial value is stored in variable t, which is initialized to f before the loop begins. Here is the full code:

2024_12-Exercise-a.c

#include <stdio.h>

int main()
{
    int f,t;

    /* prompt */
    printf("Enter a positive integer: ");
    scanf("%d",&f);
    if( f<1 ) return -1;

    /* output results */
    printf("%d!! = ",f);
    t = f;
    while(f>2)
        t *= (f-=2);
    printf("%d\n",t);

    return 0;
}

The expression t *= (f-=2); works regardless of whether the original value is odd or even. In either case, the value is reduced by two every spin of the loop. When it’s less than 2, the loop stops. Here’s a sample run:

Enter a positive integer: 8
8!! = 384

In the original post, I mentioned how recursion is often used to calculate a factorial. In fact, factorials are a great tool to use to demonstrate recursion. So, I sat back, looked at my first solution (above), and challenged myself to code a recursive version.

Recursion requires a recursive function to call. The puzzle is how to set the expression t *= (f-=2); within the recursive function, not to mention how to write the thing so that it properly unwinds. Here’s what I came up with:

2024_12-Exercise-b.c

#include <stdio.h>

int double_factorial(int a)
{
    if( a>2 )
        return( a * double_factorial(a-2) );
    return(a);
}

int main()
{
    int f;

    /* prompt */
    printf("Enter a positive integer: ");
    scanf("%d",&f);
    if( f<1 ) return -1;

    /* output results */
    printf("%d!! = %d\n",f,double_factorial(f) );

    return 0;
}

In the double_factorial() function, the recursion continues as long as the value passed (variable a) is greater than 2. The expression fits into a return statement, so the calculations are done (and returned) when the function unwinds. The final return statement passes back the result — which works!

This solution surprised me because I wrote it as presented without really thinking about it. Normally I suffer through some mental anguish to ensure that recursion doesn’t blow up. But this time I just wrote it, crossed my fingers, and built the code. My success might mean that I’m so used to writing recursive functions that I’m just good at it or — more likely — I got lucky. Either way, the code works and I am pleased.

Factorials are often used in probability and statistics. I don’t know where double factorials fit into reality because I couldn’t understand most of the Wikipedia page on the topic. Still, the goal here is to code a program to output a double factorial. You solution may not look exactly as mine does, it need only run and output the proper result.

Leave a Reply