Created
September 2, 2014 11:00
-
-
Save mihids/b19799913b2e50aa3863 to your computer and use it in GitHub Desktop.
Factorial for any number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: main.cpp | |
* Author: mihiranad | |
* | |
* Created on August 28, 2014, 12:18 PM | |
*/ | |
#include <cstdlib> | |
#include <cstring> | |
#include <iostream> | |
#include <cmath> | |
#include <list> | |
#include <assert.h> | |
#include <boost/lexical_cast.hpp> | |
using namespace std; | |
int stringToInt(std::string num) { | |
int number; | |
try { | |
number = boost::lexical_cast<int>(num); | |
} catch (boost::bad_lexical_cast&) { | |
number = -1; | |
} | |
return number; | |
} | |
int charToInt(char num) { | |
int number; | |
try { | |
number = boost::lexical_cast<int>(num); | |
} catch (boost::bad_lexical_cast&) { | |
number = -1; | |
} | |
return number; | |
} | |
std::string intToString(unsigned int num) { | |
std::string number; | |
try { | |
number = boost::lexical_cast<std::string>(num); | |
} catch (boost::bad_lexical_cast&) { | |
number = -1; | |
} | |
return number; | |
} | |
void printArr(const unsigned int* arr, const unsigned int len) { | |
for (int i = 0; i < len; i++) | |
std::cout << arr[i]; | |
std::cout << std::endl; | |
} | |
void addToIndex(unsigned int* pArr, unsigned int index, unsigned int number) { | |
unsigned int val = pArr[index] + number; | |
unsigned int res = val % 10; | |
unsigned int carry = val / 10; | |
pArr[index] = res; | |
if (carry == 0) { | |
return; | |
} else | |
addToIndex(pArr, index - 1, carry); | |
} | |
std::string multiply(std::string num1, std::string num2) { | |
unsigned int uiNum1Len = num1.length(); | |
unsigned int uiNum2Len = num2.length(); | |
int *pNum1Arr = new int[uiNum1Len]; | |
int *pNum2Arr = new int[uiNum2Len]; | |
for (int i = 0; i < uiNum1Len; i++) | |
pNum1Arr[i] = charToInt(num1[i]); | |
for (int i = 0; i < uiNum2Len; i++) | |
pNum2Arr[i] = charToInt(num2[i]); | |
unsigned int uiResLen = uiNum1Len + uiNum2Len; | |
unsigned int *pRes = new unsigned int[uiResLen]; | |
for (int i = 0; i < uiResLen; i++) | |
pRes[i] = 0; | |
unsigned int uiIndex = uiResLen - 1; | |
unsigned int uiInternalIndex; | |
for (int i = (uiNum1Len - 1); i >= 0; i--) { | |
uiInternalIndex = uiIndex--; | |
unsigned int digit1 = pNum1Arr[i]; | |
for (int j = (uiNum2Len - 1); j >= 0; j--) { | |
unsigned int digit2 = pNum2Arr[j]; | |
unsigned int res = digit1 * digit2; | |
addToIndex(pRes, uiInternalIndex, res); | |
uiInternalIndex--; | |
} | |
} | |
std::string sRes; | |
for (int i = 0; i < uiResLen; i++) | |
sRes.append(intToString(pRes[i])); | |
//printArr(pRes, uiNum1Len + uiNum2Len); | |
return sRes; | |
} | |
std::string add(std::string num1, std::string num2) { | |
if (num1.size() < num2.size()) { | |
while (num1.size() < num2.size()) | |
num1 = "0" + num1; | |
} else if (num2.size() < num1.size()) { | |
while (num2.size() < num1.size()) | |
num2 = "0" + num2; | |
} | |
unsigned int uiLen = num1.length(); | |
int *pNum1Arr = new int[uiLen]; | |
int *pNum2Arr = new int[uiLen]; | |
for (int i = 0; i < uiLen; i++) { | |
pNum1Arr[i] = charToInt(num1[i]); | |
pNum2Arr[i] = charToInt(num2[i]); | |
} | |
unsigned int uiResLen = uiLen + 1; | |
unsigned int *pRes = new unsigned int[uiResLen]; | |
for (int i = 0; i < uiResLen; i++) | |
pRes[i] = 0; | |
unsigned int uiIndex = uiResLen - 1; | |
for (int i = uiLen - 1; i >= 0; i--) { | |
unsigned int digit1 = pNum1Arr[i]; | |
unsigned int digit2 = pNum2Arr[i]; | |
unsigned int res = digit1 + digit2; | |
addToIndex(pRes, uiIndex, res); | |
uiIndex--; | |
} | |
// printArr(pRes, uiResLen); | |
// std::cout << num1 << std::endl; | |
// std::cout << num2 << std::endl; | |
std::string sRes; | |
for (int i = 0; i < uiResLen; i++) | |
sRes.append(intToString(pRes[i])); | |
//printArr(pRes, uiNum1Len + uiNum2Len); | |
return sRes; | |
} | |
std::string factorial(unsigned int num) { | |
if (num == 1) | |
return "1"; | |
std::string sNum = intToString(num); | |
return multiply(sNum, factorial(num - 1)); | |
} | |
std::string removePrecedingZeros(std::string sNum) { | |
while (charToInt(sNum[0]) == 0) { | |
sNum = sNum.substr(1, sNum.size()); | |
} | |
return sNum; | |
} | |
int main(int argc, char** argv) { | |
std::string fact; | |
std::string sum = "0"; | |
for (unsigned int i = 1; i < 16; i++) { | |
fact = factorial(i); | |
sum = add(sum, fact); | |
sum = removePrecedingZeros(sum); | |
} | |
std::cout << sum << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment