From 10c02cfbe46f869588f65b5350303f6914fe25c5 Mon Sep 17 00:00:00 2001 From: Kaspars Date: Mon, 30 Mar 2015 01:19:47 +0300 Subject: [PATCH] Factorial --- README.md | 37 ++++++++++++++++++++++++++----------- bigint.cpp | 25 +++++++++++++++++++------ bigint.h | 3 ++- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9f71464..0306deb 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,14 @@ * [Streaming operators](#streaming-operators) * [Methods](#methods) * [clear](#clear) - * [to_string](#toString) * [abs](#abs) - * [pow](#pown) + * [pow](#powint) * [digits](#digits) - * [trailing_zeros](#trailingZeros) + * [trailing_zeros](#trailing_zeros) +* [Functions](#functions) + * [abs](#absbigint) + * [to_string](#to_stringbigint) + * [factorial](#factorialint) #Description Bigint class provides math operations for arbitrarily large numbers. You know the limit is reached when your computer freezes. @@ -79,20 +82,13 @@ cout << a.pow(486);; //1459889316343... a.clear(); cout << a; //0 ``` -##to_string() -Convers Dodecahedron::Bigint to a string. -```C++ -string str; -Dodecahedron::Bigint a = 4558; -str = a.pow(486).to_string(); -``` ##abs() Absolute value. ```C++ Dodecahedron::Bigint a = -4558; cout << a.abs() // 4558 ``` -##pow(n) +##pow(int) Raises to the power of N. ```C++ Dodecahedron::Bigint a = 4558; @@ -111,4 +107,23 @@ Returns the number of trailing zeros. Dodecahedron::Bigint a = 4558; a.pow(486); cout << a.trailing_zeros(); //972 +``` +#Functions +##abs(Bigint) +Same as [abs](#abs), but returns a new instance; +```C++ +Dodecahedron::Bigint a = -455897864531248; +cout << abs(a) // 455897864531248 +``` +##to_string(Bigint) +Converts the big integer to a string. +```C++ +string str; +Dodecahedron::Bigint a = 455897864531248; +str = to_string(a); +``` +##factorial(int) +Returns a factorial of an integer, aka n! +```C++ +cout << Dodecahedron::factorial(a); //70`000+ digit number ``` \ No newline at end of file diff --git a/bigint.cpp b/bigint.cpp index be925ec..7299f9d 100644 --- a/bigint.cpp +++ b/bigint.cpp @@ -230,7 +230,7 @@ namespace Dodecahedron { //Access int Bigint::operator[](int const &b) { - return to_string()[b]-'0'; + return to_string(*this)[b]-'0'; } //Trivia @@ -264,11 +264,6 @@ namespace Dodecahedron { positive = true; skip = 0; } - std::string Bigint::to_string() { - std::ostringstream stream; - stream << *this; - return stream.str(); - } Bigint& Bigint::abs() { positive = true; return *this; @@ -322,4 +317,22 @@ namespace Dodecahedron { Bigint abs(Bigint value) { return value.abs(); } + std::string to_string(Bigint value) { + std::ostringstream stream; + stream << value; + return stream.str(); + } + Bigint factorial(int n) { + Bigint result = 1; + if(n % 2) { + result = n; + n--; + } + int last = 0; + for(; n >= 2; n -= 2) { + result *= n+last; + last += n; + } + return result; + } } diff --git a/bigint.h b/bigint.h index 2521a1c..21c4c71 100644 --- a/bigint.h +++ b/bigint.h @@ -52,7 +52,6 @@ namespace Dodecahedron { //Helpers void clear(); - std::string to_string(); Bigint& abs(); //Power @@ -66,6 +65,8 @@ namespace Dodecahedron { int compare(Bigint const &b) const; //0 a == b, -1 a < b, 1 a > b }; Bigint abs(Bigint value); + std::string to_string(Bigint value); + Bigint factorial(int n); } #endif