The root of the problem is this:
Code:
term= (double)pow(n,k)/factoriel(k) ;
The largest value a double can hold is roughly 1.7976931348623157 x 10^308.
Adding the following printf line just below the term = ... line:
Code:
printf("n = %d, k = %d, pow(n,k) = %e, fact(k) = %e, term = %e\n", n, k, pow(n, k), factoriel(k), term);
Here's what happens when I entered 62:
Code:
n = 62, k = 170, pow(n,k) = 5.088470e+304, fact(k) = 7.257416e+306, term = 7.011408e-03
n = 62, k = 171, pow(n,k) = 3.154851e+306, fact(k) = inf, term = 2.542148e-03
n = 62, k = 172, pow(n,k) = inf, fact(k) = inf, term = inf
Woops! pow(62,172) and 172! are too large to fit in a double. You get an result of "inf" or infinity, which borks all the rest of your calculations, including your "break out of the loop" check, hence the infinite loop.
I would also wager that it is a stack overflow on the recursive call that causes the crash, before k has a chance to overflow. Think, for a 32-bit system, k increments to over 2 billion before overflowing. That means a call to factoriel would have 2 billion instances on the stack. Assuming Intel for this example, you would need 12 bytes (4 for n, 4 for return address, 4 for old base pointer), that's 12*2 billion or more than 24GB of stack space required. I'm guessing you run out of stack long before k hits 2 billion.
Actually, a quick test, adding this to the top of main:
Code:
for (int i = 10000; i < 2000000000; i += 1000) {
printf("i = %d\n", i);
factoriel(i);
}
Causes the following output:
Code:
i = 219000
Segmentation fault
Crashed my system somewhere between 218000! and 219000!
To avoid calculating factorial on a negative number, just use unsigned types.