• ### What is your GameDev Story?

Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Optimised factorial funcion

This topic is 5433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having trouble with a factorial recursive funcion. Every time i imput a value greater or equal to 1000, the program crashes or returns a erroneous answer. i was wandering if any of you by any case have come about with a very eficient factorial funcion that you can share with me.

##### Share on other sites
1000! is huge. What data type are you using?

I believe the max range for floating point types are as follows:
float xE38
double xE308
long double xE4932 (when supported)

lonesock

Piranha are people too.

##### Share on other sites
Crashing has nothing to do with the numberical type he's using, it's his stack space that he's running out of (a common occurance under deeply recursive functions).

Otherwise, he may be using a data type too small to contain the final result.

[edited by - sordid on March 4, 2004 7:56:05 PM]

##### Share on other sites
A recursive factorial function will usually run out of stack space and crash because so many stack frames are necessary. An iterative version will be much more efficient, and won''t kill your program. Keep in mind, though, as lonesock pointed out, that 1000! is a huge number, and it may be that no built-in data type is sufficient to contain it. If you are using, say, C or C++, you will probably need something like the GNU MP to hold the result ...

##### Share on other sites
Doesn''t factorial get too large around 30! for example? 1000 is just nuts... I don''t understand how you''d expect that NOT to return an incorrect result.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634084
• Total Posts
3015403
×