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
6 Years
Discussion Span
Last Post by polinolin

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.' ';
}
}``````
great solution

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