Generate large proportion prime and integer numbers

Asked

Viewed 66 times

-2

How would I generate 300-digit Primes for example in PHP? And integers of the same ratio? I need them for use in encryption with Diffie-Hellman.

  • 4

    It’s easier to get a list of numbers ready instead of calculating.

  • 1

    http://www.floatingdoghead.net/bigprimes.html

  • @bfavaretto in any hypothesis! It is not for experiment, it is for use in practice.

  • 6

    Under no circumstances why? The link I passed is for precisely for use with the algorithm you want. The primes you use in the algorithm need not be secret.

  • 3

    Generating primes in PHP is either a lack of knowledge, or tools, or both. Take a ready database, and from there you select the ones you want, either randomly or with certain parameters. And there’s no secret prime number, so if I calculate a primes table here at home it’s gonna be the same primes that anyone else calculates.

1 answer

2

I agree with the bfavaretto that the easiest is to assemble a array with all the numbers. Real software does this. It’s only worth creating at hand if it’s exercise to learn but then you should do it. As it does not seem to be the case I suggest this solution preponderantly. Wikipedia list.

If you can use a library like the GMP can do so:

for ($i = 1; $i <= 300; ++$i) {
    echo $i = gmp_strval(gmp_nextprime($i + 1)) . "\n";
}

If you want an algorithm you can use this one which can be better optimized:

for ($i = 1; $i <= 300; $i++) {
    $cont = 0; 
    for ($j = 1; $j <= $i; $j++) {
        if($i % $j == 0) $cont++;
    }
    if ($cont == 2) print $i . "\n";
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • The second method will not reach the 300 digits, right? Note that the requirement was not <= 300, But they had 300 digits.

  • Not even the first :) Although it is possible, now that I have seen that he wants 300 digits :) I will see what I can do. Qdo I have time p/ see I restore.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.