Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added gcd method

  • Loading branch information...
commit 2ac68fb8c1804c89642d27f8c1537d0d49a0238c 1 parent 88e5265
@benadida benadida authored
Showing with 30 additions and 2 deletions.
  1. +18 −0 bigint.cc
  2. +4 −0 index.js
  3. +8 −2 test/big.js
View
18 bigint.cc
@@ -104,6 +104,7 @@ class BigInt : ObjectWrap {
static Handle<Value> Bsqrt(const Arguments& args);
static Handle<Value> Broot(const Arguments& args);
static Handle<Value> BitLength(const Arguments& args);
+ static Handle<Value> Bgcd(const Arguments& args);
};
static gmp_randstate_t * randstate = NULL;
@@ -157,6 +158,7 @@ void BigInt::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor_template, "bsqrt", Bsqrt);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "broot", Broot);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "bitLength", BitLength);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template, "bgcd", Bgcd);
target->Set(String::NewSymbol("BigInt"), constructor_template->GetFunction());
}
@@ -752,6 +754,22 @@ BigInt::BitLength(const Arguments& args)
return scope.Close(result);
}
+Handle<Value>
+BigInt::Bgcd(const Arguments& args)
+{
+ BigInt *bigint = ObjectWrap::Unwrap<BigInt>(args.This());
+ HandleScope scope;
+
+ BigInt *bi = ObjectWrap::Unwrap<BigInt>(args[0]->ToObject());
+ mpz_t *res = (mpz_t *) malloc(sizeof(mpz_t));
+ mpz_init(*res);
+ mpz_gcd(*res, *bigint->bigint_, *bi->bigint_);
+
+ WRAP_RESULT(res, result);
+
+ return scope.Close(result);
+}
+
static Handle<Value>
SetJSConditioner(const Arguments& args)
{
View
4 index.js
@@ -290,6 +290,10 @@ BigInt.prototype.nextPrime = function () {
return this.nextprime();
};
+BigInt.prototype.gcd = function (num) {
+ return this.bgcd(num);
+};
+
BigInt.fromBuffer = function (buf, opts) {
if (!opts) opts = {};
View
10 test/big.js
@@ -418,11 +418,17 @@ exports.bitlength = function () {
+ '873895833763224917704497568647701157104426'
).bitLength();
- console.log("-----" + bl + "------");
-
assert.equal(bl> 0, true);
};
+exports.gcd = function () {
+ var b1 = bigint('234897235923342343242');
+ var b2 = bigint('234790237101762305340234');
+ var expected = bigint('6');
+
+ assert.equal(b1.gcd(b2).toString(), expected.toString());
+};
+
if (process.argv[1] === __filename) {
assert.eql = assert.deepEqual;
Object.keys(exports).forEach(function (ex) {
Please sign in to comment.
Something went wrong with that request. Please try again.