Big integers and cousins

3

Is there a function that generates prime numbers and integers in considerably large sizes?

If not, how do you do it using JavaScript?

    
asked by anonymous 08.04.2015 / 22:09

4 answers

8

A solution using forge :

var bits = 1024;
forge.prime.generateProbablePrime(bits, function(err, num) {
    console.log('número primo aleatório', num.toString(16));
});

Solution credit for @dlongley this answer in SOEN.

    
08.04.2015 / 23:45
6

I already I insisted enough on the comments for you use a predefined prime table. Generating cousins in JavaScript is not efficient at all, especially if you just need to get one. If you need multi-digit numbers then it will be well slow.

Following is an adaptation of the Sieve of Eratosthenes posted by Guffa user on Code Review site :

function primosAte(n) {
  var i, j;
  var prime = new Array(n);
  for (i = 2; i < n ; i++) prime[i] = true;
  
  for (i = 2; i * i < n ; i++) {
    if (prime[i]) {
      for (j = 0; i * i + i * j < n ; j++) {
        prime[i * i + i * j] = false;
      }
    }
  }
  var primes = [];
  for (i = 2 ; i < n ; i++) {
    if (prime[i]){
      primes.push(i);
    }
  }
  return primes;
}
 
var primos = primosAte(1000);
document.body.innerHTML = primos.toString(', ');
    
08.04.2015 / 23:32
2

You can use one of the libraries on this page to solve your problem:

- Translated transcription of part -

The jsbn library API is highly reminiscent of Java's java.math.BigInteger classes. For example:

x = new BigInteger("abcd1234", 16);
y = new BigInteger("beef", 16);
z = x.mod(y);
alert(z.toString(16));

will print b60c .

Core Library

  • jsbn.js - basic BigInteger ployment, enough for RSA encryption and not much more.
  • jsbn2.js - the rest of the library, including most public methods of BigInteger .

RSA

  • rsa.js - implementation of RSA encryption, does not require jsbn2.js. li>
  • rsa2.js - rest of the RSA algorithm, including decryption and key generation. / li>

ECC

  • ec.js - mathematics of elliptic curves, depends on jsbn.js and jsbn2. js
  • sec.js - standard parameters of elliptic curves

Utilities

  • rng.js - RNG entropy collector interface, requires a PRNG backend to define prng_newstate ().
  • prng4.js - PRNG backend based on ARC4 for rng.js, very small.
  • base64.js - Base64 encoding and decoding routines.
  • sha1.js - SHA-1 hash function, is only required for IBE demo.

- End of transcript -

Algorithms for generating very large primes

I found a implementation of a python algorithm

    
08.04.2015 / 23:19
-1


function primosAte(n) { var i, j; var prime = new Array(n); for (i = 2; i < n ; i++) prime[i] = true;

for (i = 2; i * i < n ; i++) {
if (prime[i]) {
  for (j = 0; i * i + i * j < n ; j++) {
    prime[i * i + i * j] = false;
  }
}
}
var primes = [];
for (i = 2 ; i < n ; i++) {
  if (prime[i]){
  primes.push(i);
}
}
return primes;

}

var primes =    primosAte (100000000000000000000000000000000000000000000000000000000000000);    document.body.innerHTML = primos.toString (',');

    
18.09.2017 / 12:56