[RESOLVED] factorial
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Oct 2010
Posts
98

## [RESOLVED] factorial

hi all,

Please tell me the logic by which i can calculate the factorial of a number, my own logic has failed.

many thanks!!

2. Senior Member
Join Date
Jan 2009
Posts
1,689

## 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.

3. Member
Join Date
Oct 2010
Posts
98

## Re: factorial

void func(int num)
{
int result;
for(int i=1; i<=num; i++)
{
result=result*i;
}
}

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!!!

4. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,734

## Re: factorial

You did not initialise the result variable.

5. Senior Member
Join Date
Jan 2009
Posts
1,689

## 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 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.

6. Member
Join Date
Oct 2010
Posts
98

## Re: factorial

Thanks both of you..

7. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: factorial

ninja's code is far more than you need for a typical homework assignment, FYI.

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•