简体   繁体   English

javascript 中的非常大的数字

[英]Extremely large numbers in javascript

I'm working on the Project Euler problems (currently question 13 ).我正在处理 Project Euler 问题(当前问题 13 )。

For this question I have to find the first 10 digits of the sum of 100 numbers all of a size similar to this:对于这个问题,我必须找到大小与此类似的 100 个数字之和的前 10 位数字:

91,942,213,363,574,161,572,522,430,563,301,811,072,406,154,908,250

I think I could use something like Java's BigInteger, but I started solving the problems in JavaScript (I'm trying to boost my js abilities for work), and I would like to continue using it, even to solve this problem.我想我可以使用 Java 的 BigInteger 之类的东西,但我开始用 JavaScript 解决问题(我正在努力提高我的 js 工作能力),我想继续使用它,甚至解决这个问题。

I'd like to stick to pure JS if possible.如果可能的话,我想坚持使用纯 JS。

You are going to need a javascript based BigInteger library.您将需要一个基于 javascript 的 BigInteger 库。 There are many to choose from.有很多可供选择。 Here is one https://github.com/peterolson/BigInteger.js这是一个https://github.com/peterolson/BigInteger.js

You can use it like this你可以像这样使用它

var n = bigInt("91942213363574161572522430563301811072406154908250")
    .plus("91942213363574161572522430563301811072406154908250");

Javascript recently got a new primitive data type BigInt (stage 4 proposal as of January 2020). Javascript 最近获得了一种新的原始数据类型BigInt (截至 2020 年 1 月的第 4 阶段提案)。 https://github.com/tc39/proposal-bigint https://github.com/tc39/proposal-bigint

Chrome, Firefox and few other browsers have started supporting this in newer versions ( check compatibility here ), while other browsers are still implementing it. Chrome、Firefox 和少数其他浏览器已开始在较新版本中支持此功能( 在此处检查兼容性),而其他浏览器仍在实现它。

https://developers.google.com/web/updates/2018/05/bigint https://developers.google.com/web/updates/2018/05/bigint

Basically it can be declared using either literals like基本上它可以使用任何文字来声明,例如

var a = 1n;

or或者

var b = BigInt('22222222222222222222222222222222');

Math operators don't do auto conversion between BigInt and Number, so数学运算符不会在 BigInt 和 Number 之间进行自动转换,因此

1 + 1n

will throw an error.会抛出错误。

You could always convert your sum to a string , rip out the .您始终可以将您的总和转换为string ,将. and grab the result - something like this:并获取结果 - 像这样:

var sum = 2384762348723648237462348;
sum = sum.toString(); // "2.3847623487236483e+24"

// Rip out the "."
sum = sum.substr(0, 1) + sum.substr(2);

// Grab the first 10 characters
var firstTen = sum.substr(0, 10);

Surprisingly, sticking all the values in an array and adding them all together and just taking the first 10 digits worked.令人惊讶的是,将所有值粘贴在一个数组中并将它们加在一起,然后只取前 10 位数字就可以了。 I must have had a typo somewhere in my code when it didn't work before.当我的代码之前不起作用时,我一定在某处输入了拼写错误。

I'm sure that doing something this simple wouldn't work in all cases (like those @AlexMcmillan and @zerkms have been debating about).我敢肯定,做这么简单的事情不会在所有情况下都有效(就像@AlexMcmillan 和 @zerkms 一直在争论的那样)。 I think the safest bet is the BigInteger library mentioned by @bhspencer, but it seems like adding the first x significant digits with y digits as a buffer might also be worth a shot in some cases.我认为最安全的选择是@bhspencer 提到的 BigInteger 库,但似乎在某些情况下,将前 x 位有效数字与 y 位数字添加为缓冲区也可能值得一试。

I did this using an array and updating all entries with a function.我使用一个数组并用一个函数更新所有条目。

function f(a) {
  for (let i = 0; i < a.length - 1; i++) {
      a[i + 1] = a[i + 1] + parseInt(a[i] / 10);
      a[i] = a[i] % 10;
  }
  return a;
}
// remember to init the array with enough elements for all digits
var a = Array(200);
a.fill(0);
a[0] = 1;

Here is a JSFiddle with the code for problem 20.是一个带有问题 20 代码的 JSFiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM