Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get digits from a BigInt in javascript?

Tags:

I am working on problem n°104 of project Euler Problem 104 and would like to do it in javascript.

In order to solve this problem I need to compute large values of the Fibonacci sequence, but the numbers produced by this sequence are too large to be handle by classic Number, so I'm using BigInt supported in the latest versions of javascript.

Once I've got a particular result stored in a BigInt, I need to check it's 10 first, and last digits.


To get the digits from a Number we usually do something like in the code below, but when the number becomes very large, things go wrong:

let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result

It seems like the "toString()" methods is only using the precision of the Number type (2^53 I believe), thus we are quickly losing precision on the last digits of the BigInt number. The problem is I can't find other methods to extract those digits.

Edit : I need the precision to be perfect because basicaly what i'm doing for example is :

Compute Fibonacci(500) = 280571172992510140037611932413038677189525

Get the 10 last digits of this number : 8677189525 (this is where is lose the precision)

And then to solve my problem I need to check that those 10 last digits contains all the digits from 1 to 9

like image 462
S. Sylvain Avatar asked Mar 09 '19 00:03

S. Sylvain


People also ask

How do you convert BigInt to number?

Turns out it's as easy as passing it to the Number constructor: const myBigInt = BigInt(10); // `10n` also works const myNumber = Number(myBigInt);

Does JavaScript support BigInt?

BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 253-1. The largest number that JavaScript can reliably represent with the Number primitive is 253-1, which is represented by the MAX_SAFE_INTEGER constant.

How many bits is BigInt in JavaScript?

The BigInt64Array flavor ensures that its values remain within the signed 64-bit limit. // signed 64-bit integer.

How do you convert BigInt to string?

prototype. toString() The toString() method returns a string representing the specified BigInt value.


1 Answers

For big numbers, I think you should add the n suffix:

let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason 
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result
like image 55
Austin Greco Avatar answered Oct 13 '22 03:10

Austin Greco