Skip to content

Commit

Permalink
Issue #4: nextPrime should return prime > input, even if input is prime
Browse files Browse the repository at this point in the history
  • Loading branch information
vukicevic committed Sep 12, 2014
1 parent 72176b2 commit 262d720
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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 <vukicevic@gmail.com>"
Expand Down
19 changes: 16 additions & 3 deletions crunch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
5 changes: 4 additions & 1 deletion test/crunch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 262d720

Please sign in to comment.