Skip to content

Commit

Permalink
Factorial
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsklavins committed Mar 29, 2015
1 parent ad09224 commit 10c02cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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
```
25 changes: 19 additions & 6 deletions bigint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace Dodecahedron {

//Helpers
void clear();
std::string to_string();
Bigint& abs();

//Power
Expand All @@ -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

0 comments on commit 10c02cf

Please sign in to comment.