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..

Recommended Answers

All 4 Replies

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.' '; 
    }
 }
commented: great solution +7

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!

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 . " ";
    }
 }
?>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.