| | |
help with factorial recursive
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
Views: 833 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct studio temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






