Last active
August 29, 2015 14:08
-
-
Save fakeheal/9f6424091f4f70b5dc84 to your computer and use it in GitHub Desktop.
PHP: Large sumLongest Collatz sequence - ProjectEuler Problem #14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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