Factorial in bignum library?

I think your problem is that sum is uninitialized in your operator I'm reluctant to give a partial answer, since I took the code and fiddled with it a bit, so here's my version which appears to work (and also prints "0" correctly): Update: I couldn't resist making several structural improvements. I didn't touch your multiplication routine, so you should still be able to extract the bugfix even if you don't care for the rest include #include #include using namespace std; class bignum { public: int number100; bignum() { std::fill(number, number + 100, 0); } bignum(const bignum & other) { std::copy(other. Number, other.

Number + 100, number); } bignum(const std::string &); bignum & operator-=(const bignum &); inline bignum operator-(const bignum & c) const { return bignum(*this) -= c; } bignum operator*(const bignum &) const; inline bignum & operator= (const std::string & k) { return *this = bignum(k); } inline operator bool() const { for (size_t a = 0; a = 0; --i) { if (numberi =0; i--) { for(int j=i,x=99; j>=0; j--,x--) { ans_gridlinesj=(numberi*b. Numberx); } lines++; } //------------------------------------------------Carry Forward and assign to ans------------------------------------------------// for(int j=99; j>=0; j--) { for(int i=99; i>=0; i--) { if(ans_gridji>9 && i! =0) { carry=(ans_gridji/10); ans_gridji-1+=carry; ans_gridji%=10; } } } for(int col=99; col>=0; col--) { for(int row=99; row>=0; row--) { sumcol+=ans_gridrowcol; } } for(int i=99; i>=0; i--) { if(sumi>9 && i!

=0) { carry=(sumi/10); sumi-1+=carry; sumi%=10; } } for(int l=0; l.

I think your problem is that sum is uninitialized in your operator*. I'm reluctant to give a partial answer, since I took the code and fiddled with it a bit, so here's my version which appears to work (and also prints "0" correctly): Update: I couldn't resist making several structural improvements. I didn't touch your multiplication routine, so you should still be able to extract the bugfix even if you don't care for the rest.

#include #include #include using namespace std; class bignum { public: int number100; bignum() { std::fill(number, number + 100, 0); } bignum(const bignum & other) { std::copy(other. Number, other. Number + 100, number); } bignum(const std::string &); bignum & operator-=(const bignum &); inline bignum operator-(const bignum & c) const { return bignum(*this) -= c; } bignum operator*(const bignum &) const; inline bignum & operator= (const std::string & k) { return *this = bignum(k); } inline operator bool() const { for (size_t a = 0; a = 0; --i) { if (numberi =0; i--) { for(int j=i,x=99; j>=0; j--,x--) { ans_gridlinesj=(numberi*b.

Numberx); } lines++; } //------------------------------------------------Carry Forward and assign to ans------------------------------------------------// for(int j=99; j>=0; j--) { for(int i=99; i>=0; i--) { if(ans_gridji>9 && i! =0) { carry=(ans_gridji/10); ans_gridji-1+=carry; ans_gridji%=10; } } } for(int col=99; col>=0; col--) { for(int row=99; row>=0; row--) { sumcol+=ans_gridrowcol; } } for(int i=99; i>=0; i--) { if(sumi>9 && i! =0) { carry=(sumi/10); sumi-1+=carry; sumi%=10; } } for(int l=0; lNumberl=suml; //-------------------------------------------------------------------------------------------------------------------------------// return (ans); } bignum factorial(bignum a) { bignum j; j = "1"; bignum fact; fact="1"; while(a) { fact = fact * a; a = a-j; } return fact; } int main(int argc, char * argv) { if (argc.

Ran this program: #include "bignum. H" //the code that I pasted above int main() { bignum k; k="43"; k=k*k; k.output(); system("PAUSE"); return 0; }' Gives output as 1849. So whats broken :S – viraj Jun 22 at 3:03 I compiled using -W -Wall -Wextra -pedantic and all I got was two or three warning about unused variables.

– viraj Jun 22 at 3:09 Whoops, confused true and false in the bool operator! Fixing... – Kerrek SB Jun 22 at 3:10 @viraj: No idea. Also use valgrind to check for uninitialized memory access.My version works (almost; your choice of going backwards is really awkward, I have to type "34" to get "43").

Result for 43! Is 60415263063373835637355132068513997507264512000000000. – Kerrek SB Jun 22 at 3:12 Odd, because when I assign 43 to a string, it takes 43 only.

:| what compiler do you use? – viraj Jun 22 at 13:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions