How to use the Maximum Common Divisor (GCD) function in Bigint.js

Asked

Viewed 405 times

3

I’m trying to use the function GCD library Bigint.js. But somehow, I try to use raw numbers, but it returns 0

In the description it 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 results 0. How to use it correctly?

2 answers

4


By the library code, on the same link you passed, you can see that the function GCD expects you to pass two numbers in format bigInt, and non-numeric types of Javascript.

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

  • 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 the integer t, with at least n bits and an array of m elements

After obtaining MDC, it is possible to convert the bigInt to string and visualize its value, using the function bigInt2str:

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

    // string bigInt2str(x,base) //Return a string form of bigint x in a Given base, with 2 <= base <= 95

    translating:

    // string bigInt2str(x,base) // returns the string form from a bigint x in the given base, where 2 <= base <= 95

In short, for your code to work, you could do so:

var mdc = GCD(int2bigInt(1124,1,1), int2bigInt(2048,1,1));
var str = bigInt2str(mdc, 10);
document.getElementById("resultado").innerHTML = (str);
<script src="http://www.leemon.com/crypto/BigInt.js"></script>
<div id="resultado"></div>

  • you could tell me what is bigint?

  • It is a large integer, larger than the maximum Javascript supported (which is 9007199254740991, or 2 53-1, represented in the JS in a structure equivalent to a Double, and insufficient for the encryption functions you are trying to implement).

  • @bfavaretto a number? To half lost..

  • 2

    Yes, a number! Now I’m the one who’s lost, what you hoped it would be?

  • is why in the answers they said they had to transform the number in the format of bigint.. When I said it was a number I was half Hi? @bfavaretto

  • A bigInt in this library is a data structure used to represent a large integer, that the JS is not able to represent with its type Number. Therefore, a bigInt represents a number. @user3163662

  • Ahhh, it’s like a smaller size "camouflage" since javascript can’t support more than a "double"? @bfavaretto

  • 1

    In fact they appear to be arrays, which represent such numbers.

Show 3 more comments

2

The GCD function needs its parameters to be of the type bigInt, and not whole. 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.