Show us your code and we'll tell you what you did wrong. Factorial is very easy, and I have a really good method for it, but this sounds like homework.
Last edited by ninja9578; October 23rd, 2010 at 10:13 AM.
You're also missing a few special cases, you also probably don't want to use an int as you'll run out of room REALLY fast. Here is my function:
Just change number_t to whatever type you want, and adjust MAX_FACTORIAL to your liking, or leave it. Computers have memory to burn, and even though some computers and compilers will run out of range long before 1755, none will run beyond it.
Code:
typedef long double number_t; //replace with whatever you want
#define MAX_FACTORIAL 1755 //Use getMax to figure out what to set this to, 1755 is for 128-bit long doubles
number_t factorial(int n){
//Static Lookup tables and checking for static constants
static number_t preCalculated[MAX_FACTORIAL] = {0}; //Set everything to 0, since it is an impossible result
#ifndef INFINITY
const static number_t INFINITY = (number_t)(1.0f/0.0f);
#endif
#ifndef NAN
const static number_t NAN = (number_t)sqrt(-1);
#endif
//Check special cases
if (n > MAX_FACTORIAL - 1){ //Know it's going to overflow, so don't bother calculating
return INFINITY;
} else if (n < 0){ //Factorial of negative numbers in undefined
return NAN;
} else if (n < 2){ //0! and 1! both resolve to 1
return 1;
} else if (preCalculated[n] != 0){ //already solved for this value, why solve again?
return preCalculated[n];
}
//Need to calculate it
number_t result = (number_t)n;
for (unsigned int value = (unsigned int)n - 1; value > 1; --value){
result *= (number_t)value;
}
//store the result for later and return
preCalculated[n] = result;
return result;
}
Code:
//Use this to determine what to define MAX_FACTORIAL to be, then you can remove it
unsigned int getMax(void){
for (unsigned int n = 2; n < 5000; ++n){
number_t result = (number_t)n;
for (unsigned int value = n - 1; value > 1; --value){
result *= (number_t)value;
}
if (!isfinite(result)) return n;
}
return 5000;
}
Last edited by ninja9578; October 24th, 2010 at 08:32 AM.
#ifndef INFINITY
const static number_t INFINITY = (number_t)(1.0f/0.0f);
#endif
#ifndef NAN
const static number_t NAN = (number_t)sqrt(-1);
#endif
VC++ 2010 didn't even compile your INFINITY: error C2124 - guess what the message text was...
The NAN was rejected with a complaint about an ambiguous overload resolution. After changing the numeric literal to -1.0 it compiled and cout gave me -1.#IND as the output for that. I didn't try that however with <complex> included and thus don't know if that would have made any difference, as we most likely all know that sqrt(-1) is i from the math point of view.
The processor manuals for both the 486 and the Pentium 4 I consulted about that weren't really clear about what the FPU would make out of this input.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Bookmarks