943,706 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1398
  • C++ RSS
Dec 30th, 2008
0

help with factorial recursive

Expand Post »
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 30th, 2008
0

Re: help with factorial recursive

OK here is a little improved version of it. Now it finds at maximum of
65!. Any hints on how to find 100!
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 30th, 2008
0

Re: help with factorial recursive

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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 30th, 2008
0

Re: help with factorial recursive

checked again anything over 20! is not correct. anyhelp?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 31st, 2008
0

Re: help with factorial recursive

Check out GMP (Gnu MultiPrecision). It allows you to work with extremely large numbers and has wrappers for c++.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 31st, 2008
1

Re: help with factorial recursive

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).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to change scientific form into decimal form.
Next Thread in C++ Forum Timeline: Reading Data from text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC