| | |
help with factorial recursive
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<cmath> #include<iomanip> using namespace std; unsigned __int64 Fact(unsigned __int64 x ); int main(){ /*n! means n (n 1) ... 3 2 1 Find the sum of the digits in the number 100!*/ cout<<Fact(100); } unsigned __int64 Fact(unsigned __int64 x ) { __int64 num(0); if(x==0) return 1; else { num = x * Fact(x-1); cout<<num<<" "<<x<<endl; } return num; }
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?
OK here is a little improved version of it. Now it finds at maximum of
65!. Any hints on how to find 100!
65!. Any hints on how to find 100!
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<cmath> #include<iomanip> using namespace std; unsigned __int64 Fact(unsigned __int64 x ); int main(){ /*n! means n (n 1) ... 3 2 1 Find the sum of the digits in the number 100!*/ //unsigned __int64 * fact = new unsigned __int64 (10000); Fact(100); } unsigned __int64 Fact(unsigned __int64 x ) { unsigned __int64 * fact = new unsigned __int64 (10000); *fact = x; if(x==0) return 1; else { *fact = x * Fact(x-1); cout<<*fact<<" "<<x<<endl; } return *fact; delete fact; }
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.
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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).
![]() |
Similar Threads
- Order of a recursive function (C)
- How to to calculate the factorial of a given integer WITHOUT using recursion. (C++)
- Recursive Factorial (C++)
- printing recursive call parameter (C++)
- Recursive Insertion on Linked Lists (C++)
- Recursive functions??? (C)
- Factorial program (Java)
Other Threads in the C++ Forum
- Previous Thread: how to change scientific form into decimal form.
- Next Thread: Reading Data from text file
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






