Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 24th, 2005, 6:03 AM   #1
Lina
Newbie
 
Join Date: Jun 2005
Posts: 1
Rep Power: 0 Lina is on a distinguished road
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...
Lina is offline   Reply With Quote
Old Jun 24th, 2005, 8:43 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by Lina
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?
If I was to do this in C (which I wouldn't: C++ is better for managing this sort of thing, IMHO), I would use some form of data structure that is able to represent the range of integers you want. For example, a structure of the form
struct SomeType
{
    int x[2];
}
can probably store up to MAX_INT*MAX_INT distinct integers (where MAX_INT is the maximum value that can be stored in an integer). If you write a set of helper functions that allow you to manipulate such structures (eg add them, subtract them, multiply them, print them, etc etc) your problem will be solved. To store 500! you will probably need a VERY large structure, so will probably resort to a dynamically allocated array of int's (or unsigned's) in your structure, and your manipulation function will need to make the array grow as required.
grumpy is offline   Reply With Quote
Old Jun 24th, 2005, 9:34 AM   #3
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
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.
Aphex_Twin is offline   Reply With Quote
Old Jun 24th, 2005, 10:11 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 24th, 2005, 10:49 AM   #5
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
Quote:
Originally Posted by DaWei
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.
Recursion on a predefined chunk of memory with passing just a pointer at a time is basically cost-less in terms of stack space.

But yes, it can be done at about the same complexity level by iterative means.
Aphex_Twin is offline   Reply With Quote
Old Jun 24th, 2005, 11:31 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 24th, 2005, 11:38 AM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4 Ancient Dragon is on a distinguished road
This math library might help
Ancient Dragon is offline   Reply With Quote
Old Jun 24th, 2005, 1:05 PM   #8
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
all y'all's (hehe) avatars look mighty peculiar! (mine is fine )
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Jun 24th, 2005, 1:07 PM   #9
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Old Jul 3rd, 2005, 11:50 AM   #10
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4 mackenga is on a distinguished road
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.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC