Collatz Conjeture in PHP
I've made some versions of the Collatz Conjeture in PHP, this is the most simple, it didn't use any array storage to avoid filling the computer memory.
It takes 163 seconds in my MacBook to test first 10.000.000 integers.
Sample output (after 163 seconds):
ENDED CHECKING UNTIL 10000000 in 163 seconds (61349.693251534 numbers per second)
$limit=10000000;
$i=2;
$checked=array();
$start=time();
while ($i<=$limit)
{
//echo "$i : ";
if (!collatz($i))
{
echo "$i NO!\n";
die;
}
$i++;
}
echo "\n";
$end=time();
$elapsed=$end-$start;
$numbers_per_second=$limit;
if ($elapsed>0) $numbers_per_second=$limit/$elapsed;
echo "ENDED CHECKING UNTIL $limit in $elapsed seconds ($numbers_per_second numbers per second)\n";
function collatz ($i)
{
if ($i==1) return true;
//echo "$i,";
if ($i%2==0)
{// par
$i=collatz($i/2);
}
else
{// impar
$i=collatz(($i*3)+1);
}
return $i;
}