大整数斐波那契序列在第36个阶段发散:项目欧拉#25

时间:2021-08-13 16:49:55

So I am trying to solve project euler #25 via the big integer brute force method with this module. Everything seems to go properly until the 36'th term, which actually decreases. Then the terms increase as they should, and then decrease again; they never go over 10 million. I have also noticed the 36'th term has all digits correct except one as it is supposed to be 14930352 but I get 4930352 Could this be a problem with my code or a bug in the module?

所以我试图用这个模块通过大整数强力方法解决项目euler#25。一切似乎都要正常,直到第36个学期,实际上会减少。然后这些术语会按原样增加,然后再次减少;他们永远不会超过1000万。我也注意到第36个术语的所有数字都正确,除了一个因为它应该是14930352但我得到4930352这可能是我的代码或模块中的错误的问题吗?

var bigInt = require('big-integer');
var number = bigInt(1);
var last = bigInt(1);

for(i=0;i<50;i++){
  number = number.add(last);
  last = number.minus(last);
  console.log(number.toString());
}

1 个解决方案

#1


1  

Looks like a bug in the library. If you use a tmp variable it works fine.

看起来像库中的错误。如果你使用tmp变量,它工作正常。

var bigInt = require('big-integer');
var number = bigInt(1);
var last = bigInt(1);

for(i=0;i<50;i++){
  //number = number.add(last);
  //last = number.minus(last);

  var tmp = number.add(last);
  last = number;
  number = tmp;
  console.log((i + 3) + ":" + number.toString());
}

#1


1  

Looks like a bug in the library. If you use a tmp variable it works fine.

看起来像库中的错误。如果你使用tmp变量,它工作正常。

var bigInt = require('big-integer');
var number = bigInt(1);
var last = bigInt(1);

for(i=0;i<50;i++){
  //number = number.add(last);
  //last = number.minus(last);

  var tmp = number.add(last);
  last = number;
  number = tmp;
  console.log((i + 3) + ":" + number.toString());
}