Skip to content

Instantly share code, notes, and snippets.

@fakeheal
Last active August 29, 2015 14:08
Show Gist options
  • Save fakeheal/9f6424091f4f70b5dc84 to your computer and use it in GitHub Desktop.
Save fakeheal/9f6424091f4f70b5dc84 to your computer and use it in GitHub Desktop.
PHP: Large sumLongest Collatz sequence - ProjectEuler Problem #14
<?php
function sequence($start, $step = 0)
{
while($start != 1)
{
if($start % 2 == 0)
$start = $start / 2;
else
$start = 3*$start + 1;
$step++;
}
return $step;
}
for($i = 1000000; $i > 1; $i--)
{
$all_steps[$i] = sequence($i);
}
$max = array_keys($all_steps, max($all_steps));
echo 'Number: '.$max[0].' Chain: '.$all_steps[$max[0]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment