Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New pow function #10

Merged
merged 5 commits into from
Dec 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -166,7 +166,6 @@ how to contribute to the project.

This project is licensed under the terms of the [MIT license][license-link].


[release-shield]: https://img.shields.io/github/release/faheel/BigInt/all.svg?style=for-the-badge
[release-link]: https://github.com/faheel/BigInt/releases
[travis-shield]: https://img.shields.io/travis/faheel/BigInt.svg?style=for-the-badge
44 changes: 41 additions & 3 deletions include/functions/math.hpp
Original file line number Diff line number Diff line change
@@ -7,9 +7,7 @@
#ifndef BIG_INT_MATH_FUNCTIONS_HPP
#define BIG_INT_MATH_FUNCTIONS_HPP

#include "operators/relational.hpp"
#include "operators/unary_arithmetic.hpp"

#include <string>

/*
abs
@@ -33,4 +31,44 @@ BigInt big_pow10(size_t exponent) {
return BigInt("1" + std::string(exponent, '0'));
}

/*
pow
---
Return a BigInt equal to my_big_int^x
NOTE: x could be BigInt, long long, or string
*/
BigInt pow(const BigInt& my_big_int, const long long& x) {
BigInt a = my_big_int;
BigInt n = x;

if (n < 0) {
n = BigInt(1)/n;
a = -a;
}

if (n == 0) return BigInt(1);

BigInt y = 1;
while (n > 1) {
if ((n % 2) == 0) {
a *= a;
n /= 2;
} else {
y *= a;
a *= a;
n = (n-1)/2;
}
}
return a*y;
}

BigInt pow(const long long& my_big_int, const long long& x) {
return pow(BigInt(my_big_int), x);
}

BigInt pow(const std::string& my_big_int, const long long& x) {
return pow(BigInt(my_big_int), x);

}

#endif // BIG_INT_MATH_FUNCTIONS_HPP
2 changes: 1 addition & 1 deletion include/operators/binary_arithmetic.hpp
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@
#define BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP

#include <climits>
#include <cmath>
#include <string>

#include "BigInt.hpp"
@@ -20,6 +19,7 @@
#include "operators/increment_decrement.hpp"
#include "operators/relational.hpp"
#include "operators/unary_arithmetic.hpp"
#include <cmath>

const long long FLOOR_SQRT_LLONG_MAX = 3037000499;

27 changes: 26 additions & 1 deletion test/functions/math.test.cpp
Original file line number Diff line number Diff line change
@@ -3,9 +3,10 @@
#include <random>

#include "constructors/constructors.hpp"
#include "functions/conversion.hpp"
#include "functions/math.hpp"
#include "operators/io_stream.hpp"
#include "operators/binary_arithmetic.hpp"
#include "functions/conversion.hpp"

#include "third_party/catch.hpp"

@@ -61,3 +62,27 @@ TEST_CASE("Randomised test for big_pow10", "[functions][math][big_pow10][random]
REQUIRE(big_num.to_string() == "1" + std::string(rand_exponent, '0'));
}
}

TEST_CASE("Pow with BigInt", "[conversion][string]") {
BigInt a = 12;

REQUIRE(pow(a, 32ll) == "34182189187166852111368841966125056");
a = 32;
REQUIRE(pow(a, 12ll) == "1152921504606846976");
a = 50;
REQUIRE(pow(a, 23ll) == "1192092895507812500000000000000000000000");
}

TEST_CASE("Pow with long long", "[conversion][string]") {
REQUIRE(pow(20ll, 40ll) == "10995116277760000000000000000000000000000000000000000");
REQUIRE(pow(30ll, 45ll) == "2954312706550833698643000000000000000000000000000000000000000000000");
REQUIRE(pow(45ll, 30ll) == "39479842665806602234295041487552225589752197265625");
REQUIRE(pow(40ll, 20ll) == "109951162777600000000000000000000");
}

TEST_CASE("Pow with string", "[conversion][string]") {
REQUIRE(pow("33", 27ll) == "99971538734896047460249499950752967950177");
REQUIRE(pow("21", 59ll) == "1025506433613486607375777617584133309366191904729927960524981845743709132117581");
REQUIRE(pow("42", 59ll) == "591164210212831306083214062268562882225191490961799646040575789354982868342002274605481253142528");
REQUIRE(pow("50", 51ll) == "444089209850062616169452667236328125000000000000000000000000000000000000000000000000000");
}