Here's the textbook problem I need to solve:
Write a recursive function that solves the following for a given number n:
The result I get for n = 6 is 120.184056
The result I want for n = 6 is 120.184052
The result I get for n = 7 is 720.147296
The result I want for n = 7 is 720.147339
My question is, why are my decimals off? The error seems to be increasing as I increase the n.
My code:
#include <stdio.h>
#include <stdlib.h>
double factorial(int num)
{
if(num==1)
{
return 1;
}
else
{
return num * factorial(num-1);
}
}
double recurisve(int n, int c)
{
double addend, enumerator;
if(c%2==1)
{
addend = c/2+1;
enumerator = factorial(n - c/2);
}
else if(c%2==0)
{
addend = n - (c-1)/2;
enumerator = factorial((c-1)/2+1);
}
if(c==n)
{
return addend + enumerator;
}
else
{
return addend + enumerator / recurisve(n, c+1);
}
}
int main()
{
int n;
scanf("%d",&n);
printf("%lf",recurisve(n,1));
return 0;
}