diff --git a/bower.json b/bower.json index 5de5e78..ff9965c 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "crunch", "main": "crunch.js", - "version": "1.0.4", + "version": "1.0.5", "homepage": "https://github.com/vukicevic/crunch", "authors": [ "Nenad Vukicevic " diff --git a/crunch.js b/crunch.js index 64fd850..72234ce 100644 --- a/crunch.js +++ b/crunch.js @@ -618,13 +618,26 @@ function Crunch(rawIn, rawOut) { return mrb(x, 3); } - function npr(x) { + /** + * Quick add integer n to arbitrary precision integer x avoiding overflow + */ + function qad(x, n) { var l = x.length - 1; - x[l] |= 1; + if (x[l] + n < 268435456) { + x[l] += n; + } else { + x = add(x, [n]); + } + + return x; + } + + function npr(x) { + x = qad(x, 1 + x[x.length-1] % 2); while (!tpr(x)) { - x[l] = (x[l]+2) % 268435456; //backwards on boundary + x = qad(x, 2); } return x; diff --git a/package.json b/package.json index 73aba1c..6a35944 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "number-crunch", - "version": "1.0.4", + "version": "1.0.5", "description": "Fast arbitraty-precision integer arithmetic library. Used for large-number calculations including finding large prime numbers, performing modular exponentiation and other arithmetic operations.", "main": "crunch.js", "scripts": { diff --git a/test/crunch.js b/test/crunch.js index 7e6e90f..18c643e 100644 --- a/test/crunch.js +++ b/test/crunch.js @@ -369,11 +369,14 @@ describe('#next prime', function() { crunch.nextPrime([5, 57, 84, 76, 233, 0, 120, 91, 180, 180, 8]).should.eql([5, 57, 84, 76, 233, 0, 120, 91, 180, 180, 107]); }); it('Find next prime', function() { - crunch.nextPrime([17]).should.eql([17]); + crunch.nextPrime([17]).should.eql([19]); }); it('Find next prime', function() { crunch.nextPrime([7, 200]).should.eql([7, 201]); }); + it('Find next prime', function() { + crunch.nextPrime([3]).should.eql([5]); + }); }); describe('#factorial', function() {