help with factorial recursive

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1,280
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

help with factorial recursive

 
0
  #1
Dec 30th, 2008
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. #include<iomanip>
  5.  
  6. using namespace std;
  7. unsigned __int64 Fact(unsigned __int64 x );
  8.  
  9.  
  10. int main(){
  11.  
  12. /*n! means n (n 1) ... 3 2 1
  13.  
  14. Find the sum of the digits in the number 100!*/
  15.  
  16.  
  17. cout<<Fact(100);
  18.  
  19.  
  20. }
  21. unsigned __int64 Fact(unsigned __int64 x )
  22. {
  23. __int64 num(0);
  24.  
  25. if(x==0)
  26. return 1;
  27. else
  28. {
  29. num = x * Fact(x-1);
  30. cout<<num<<" "<<x<<endl;
  31. }
  32.  
  33. return num;
  34. }


my recursive function works. But only for small x. (ex. 5! =120).

but when i try (100!) it runs out of space and give NULL;

so can you hint on how i can fix the code so that the computer will compute 100factorial?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,280
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: help with factorial recursive

 
0
  #2
Dec 30th, 2008
OK here is a little improved version of it. Now it finds at maximum of
65!. Any hints on how to find 100!
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. #include<iomanip>
  5.  
  6. using namespace std;
  7. unsigned __int64 Fact(unsigned __int64 x );
  8.  
  9.  
  10. int main(){
  11.  
  12. /*n! means n (n 1) ... 3 2 1
  13.  
  14. Find the sum of the digits in the number 100!*/
  15.  
  16. //unsigned __int64 * fact = new unsigned __int64 (10000);
  17.  
  18. Fact(100);
  19.  
  20. }
  21. unsigned __int64 Fact(unsigned __int64 x )
  22. {
  23. unsigned __int64 * fact = new unsigned __int64 (10000);
  24. *fact = x;
  25.  
  26. if(x==0)
  27. return 1;
  28. else
  29. {
  30. *fact = x * Fact(x-1);
  31. cout<<*fact<<" "<<x<<endl;
  32. }
  33.  
  34. return *fact;
  35. delete fact;
  36. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 963
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: help with factorial recursive

 
0
  #3
Dec 30th, 2008
Can't you use a loop instead?

You could create a class that grows and manages the value split over vectors, overloads the general math operators you'd need, then stores the values into a file.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,280
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: help with factorial recursive

 
0
  #4
Dec 30th, 2008
checked again anything over 20! is not correct. anyhelp?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: help with factorial recursive

 
0
  #5
Dec 31st, 2008
Check out GMP (Gnu MultiPrecision). It allows you to work with extremely large numbers and has wrappers for c++.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: help with factorial recursive

 
1
  #6
Dec 31st, 2008
To give you a clue about what's happening, 100! is about 9.3 by 10^157. To represent that, a 525 bit integer is needed (and that increases 530 bits to represent 101!). 64 bit is nowhere near enough: an unsigned 64 bit integer can only represent a maximum value of about 18446744073709551615 (approx 1.8 by 10^19).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC