How to use the Maximum Common Splitter (GCD) function in BigINT.js

3

I'm trying to use the GCD library function BigINT.js . But I do not know how, I try to use raw numbers, but it returns 0

In the description says

// bigInt  GCD(x,y)
// return greatest common divisor of bigInts x and y (each with same number of elements).

And I try to use it like this:

GCD(1124,2048);

But it turns 0. How to use it correctly?

    
asked by anonymous 15.04.2015 / 00:06

2 answers

4

From the library code, in the same link you passed, you can see that the GCD function expects you to pass two numbers in the bigInt format, rather than numeric JavaScript types.

To convert numeric JavaScript types to bigInt , there is the int2bigInt function.

  • You can read the following documentation at the top of the file:

      

    // bigInt int2bigInt (t, n, m) // return a bigInt equal to integer t, with at least n bits and m array elements

    translating:

      

    // bigInt int2bigInt (t, n, m) // returns a bigInt equal to integer t, with at least n bits and an array of m elements

After obtaining the MDC, you can convert bigInt to string and display its value, using the bigInt2str function:

  • still in the documentation at the top of the file:

      

    // string bigInt2str (x, base) // return a string form of bigInt x in a given base, with 2

15.04.2015 / 00:28
2

The GCD function needs its parameters to be bigInt , not integer. If you create bigInts x and y , you can then call the function correctly:

var BI1 = int2bigInt(1124, 1, 1);
var BI2 = int2bigInt(2048, 1, 1);
var BI3 = GCD(BI1, BI2);
console.log(BI3);
    
15.04.2015 / 00:28