Bresser
March 11th, 2015, 07:01 PM
Hello so I want to start off this is not Homework.
But I am in a c++ class and we were going over recursion and a Factorial equation:
Quick sum if you don't know what a factorial is. Its like this Factorial of 6 is = 6 * 5 * 4 * 3 * 2 * 1 or 720
7 is = 7 * 6 * 5 * 4 * 3 * 2 * 1 or 5040.
So as you can imagine you hit the limits of C++ datatypes pretty fast. At 20 is the biggest you can do with int16_t and size 170 is the biggest you can do with double.
My teacher was telling us about the limitation of this and I thought I would use the bigInt library. But every time it only outputs one can anyone tell where I messed up?
// This program tests a recursive algorithm for the factorial
// function
#include
<iostream>
#include
<sstream>
#include
<stdint.h>
#include
<string>
#include
<iomanip>
#include
"BigIntegerLibrary.hh"
using
namespace std;
BigInteger Factorial (BigInteger number);
// Function prototype
int
main()
{
string theNumber;
BigInteger bigNumber;
bigNumber = stringToBigInteger(theNumber);
cout <<
"Enter whole number for factorial operation: ";
cin >> theNumber;
cout << endl << endl;
cout << theNumber <<
"! = " << Factorial(bigNumber) << endl;
system(
"pause");
return 0;
}
// This function receives an integer and calculates the
// factorial of that integer using a recursive algorithm
BigInteger Factorial (BigInteger number)
// Pre: number is assigned and number >= 0.
{
if ( number == 0) // base case
return 1 ;
else // general case
{
return number * Factorial(number - 1);
}
}
But I am in a c++ class and we were going over recursion and a Factorial equation:
Quick sum if you don't know what a factorial is. Its like this Factorial of 6 is = 6 * 5 * 4 * 3 * 2 * 1 or 720
7 is = 7 * 6 * 5 * 4 * 3 * 2 * 1 or 5040.
So as you can imagine you hit the limits of C++ datatypes pretty fast. At 20 is the biggest you can do with int16_t and size 170 is the biggest you can do with double.
My teacher was telling us about the limitation of this and I thought I would use the bigInt library. But every time it only outputs one can anyone tell where I messed up?
// This program tests a recursive algorithm for the factorial
// function
#include
<iostream>
#include
<sstream>
#include
<stdint.h>
#include
<string>
#include
<iomanip>
#include
"BigIntegerLibrary.hh"
using
namespace std;
BigInteger Factorial (BigInteger number);
// Function prototype
int
main()
{
string theNumber;
BigInteger bigNumber;
bigNumber = stringToBigInteger(theNumber);
cout <<
"Enter whole number for factorial operation: ";
cin >> theNumber;
cout << endl << endl;
cout << theNumber <<
"! = " << Factorial(bigNumber) << endl;
system(
"pause");
return 0;
}
// This function receives an integer and calculates the
// factorial of that integer using a recursive algorithm
BigInteger Factorial (BigInteger number)
// Pre: number is assigned and number >= 0.
{
if ( number == 0) // base case
return 1 ;
else // general case
{
return number * Factorial(number - 1);
}
}