hi all,
Please tell me the logic by which i can calculate the factorial of a number, my own logic has failed.
please help...
many thanks!!
Printable View
hi all,
Please tell me the logic by which i can calculate the factorial of a number, my own logic has failed.
please help...
many thanks!!
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.
ok, i've made a function:
void func(int num)
{
int result;
for(int i=1; i<=num; i++)
{
result=result*i;
}
cout<<"answer is:"<<result;
}
and i've called it in main.
void main()
{
int a;
cin>>a;
func(a);
}
Please tell me what i'm doing wrong??
many thanks!!!
You did not initialise the result variable.
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;
}
Thanks both of you.. :)
ninja's code is far more than you need for a typical homework assignment, FYI.
Hi ninja9578, I thought we were through with that since http://www.codeguru.com/forum/showthread.php?t=503561... ;)
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.