Skip to content

Instantly share code, notes, and snippets.

@Narsil
Created November 6, 2012 10:01
Show Gist options
  • Save Narsil/4023812 to your computer and use it in GitHub Desktop.
Save Narsil/4023812 to your computer and use it in GitHub Desktop.
Project euler
// Tested on http://writecodeonline.com/php/
$triangle = array(
array(
3
),array(
7, 4
),array(
2, 4, 6
),array(
8, 5, 9, 3
)
);
function somme_triangle($i, $j, $triangle){
$somme = $triangle[$i][$j];
if($i == count($triangle)){
return $somme;
}else{
$sous_triangle1 = somme_triangle($i+1, $j, $triangle);
$sous_triangle2 = somme_triangle($i+1, $j+1, $triangle);
$somme += max($sous_triangle1, $sous_triangle2);
return $somme;
}
}
echo somme_triangle(0, 0, $triangle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment