0

Hello dude. Happy New Year. I want to asking about generating primes algorithm. Can you advice tell me what the fast generating primes algorithm except Sieve of Eratosthenes algorithm? thank you. i want to apply the algorithm to my cryptographic system..

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by polinolin
2

Hope this helps, can be turned into a function easily.

$startAmount = 0;
$endAmount = 100000;

for ($i = $startAmount; $i <= $endAmount; $i++) 
{
    if($i % 2 != 1) 
    {
      continue;
    }
 
    $d = 3; 
    $x = sqrt($i); 

    while ($i % $d != 0 && $d < $x) 
    {
        $d += 2; 
    }

    if((($i % $d == 0 && $i != $d) * 1) == 0) 
    {
        echo $i.' '; 
    }
 }
Votes + Comments
great solution
0

Hope this helps, can be turned into a function easily.

$startAmount = 0;
$endAmount = 100000;

for ($i = $startAmount; $i <= $endAmount; $i++) 
{
    if($i % 2 != 1) 
    {
      continue;
    }
 
    $d = 3; 
    $x = sqrt($i); 

    while ($i % $d != 0 && $d < $x) 
    {
        $d += 2; 
    }

    if((($i % $d == 0 && $i != $d) * 1) == 0) 
    {
        echo $i.' '; 
    }
 }

Thanks dude.. Its more fast than Sieve of Eratosthenes algorithm.
its so useful!

1

Just a note, you can go a bit faster changing numbers with variables, a modified version of mikulucky:

<?php
$a = 0;
$b = 1;
$c = 2;
$d = 3;
$endAmount = 100000;

for ($i = $a; $i <= $endAmount; $i++) 
{

    if($i % $c != $b) 
    {
      continue;
    }

    $x = sqrt($i);

    while ($i % $d != $a && $d < $x)
    {
        $d += $b; 
    }

    if((($i % $d == $a && $i != $d) * $b) == $a) 
    {
	echo $i . " ";
    }
 }
?>
0

Just a note, you can go a bit faster changing numbers with variables, a modified version of mikulucky:

<?php
$a = 0;
$b = 1;
$c = 2;
$d = 3;
$endAmount = 100000;

for ($i = $a; $i <= $endAmount; $i++) 
{

    if($i % $c != $b) 
    {
      continue;
    }

    $x = sqrt($i);

    while ($i % $d != $a && $d < $x)
    {
        $d += $b; 
    }

    if((($i % $d == $a && $i != $d) * $b) == $a) 
    {
	echo $i . " ";
    }
 }
?>

thanks for your modified bro!
its so useful :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.