![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 1
Rep Power: 0
![]() |
500!
Hello all...
How we can calculate and output the value of 500!. Namely, how one can deal with a huge values which does not fit to any known data types because of it limitation? Thanks... |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
struct SomeType
{
int x[2];
} |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
500! fits on something like 137 kb. So you need to use more than two ints. I never handled such large factorials, but I did play around with multiplications of very large numbers (I got it up to 15000 decimal digits, probably could have gone MUCH further). What you need is a structure to represent large chunks of bits (preferably a memory mapped file) and a multiplication algorithm (try Karatsuba, FFT multiplication)...
The simplest way I can think of doing it is to do a recursive function to handle the factorials. The recursive definition of the factorial is: factorial(0) = 1 factorial(n+1) = factorial(n) * n+1 One thing to remember: multiplying two numbers of length a and b would get a result of size <= (a+b), so you don't have to use up all the memory from the beginning. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Caveat: the recursive approach may use up your allotted stack before it uses up the memory you've alloted for the result. Most likely not for a factorial operation, though; the result grows rapidly, to indulge in an understatement.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
Quote:
But yes, it can be done at about the same complexity level by iterative means. |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, perhaps I posted unclearly, but I thought it showed that I was speaking of recursion in general, and not as applied to the factorial problem. "Cost" is a relative term (as is "low"), but "costless" is quite absolute and inapplicable.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4
![]() |
This math library might help
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
all y'all's (hehe) avatars look mighty peculiar! (mine is fine
)
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I agree with DaWei. I had the same problem with a Hex-Dec converter where i used a recursive function to implement multiplication and I got a stack overflow error.
|
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4
![]() |
I'd use an iterative approach (in C) and store the big integers in int-sized chunks in a singly linked list. You'd only need to implement a couple of operations on these big integers anyway; might as well not implement what you aren't going to use. I tackled a similar problem (well, it was huge Fibonacci numbers rather than factorials, but with the same large integer storage problems) in Ada a couple of years ago - a class assignment at Uni. I'd see if I could dig out the solution, but it'd probably not be very useful here.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|