
October 23rd, 2010, 09:33 AM
#1
[RESOLVED] factorial
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!!

October 23rd, 2010, 10:02 AM
#2
Re: factorial
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.

October 24th, 2010, 04:11 AM
#3
Re: factorial
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!!!

October 24th, 2010, 04:21 AM
#4
Re: factorial
You did not initialise the result variable.

October 24th, 2010, 08:23 AM
#5
Re: factorial
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 128bit 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.

October 25th, 2010, 10:54 AM
#6
Re: factorial
Thanks both of you..

October 25th, 2010, 10:56 AM
#7
Re: factorial
ninja's code is far more than you need for a typical homework assignment, FYI.

October 25th, 2010, 07:55 PM
#8
Re: factorial
Hi ninja9578, I thought we were through with that since http://www.codeguru.com/forum/showthread.php?t=503561...
Originally Posted by ninja9578
Code:
#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.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
