function euler($triangle) { $count = count($triangle); if ($count == 1) { return $triangle[0]; } $up = explode(" ", $triangle[$count - 2]); $down = explode(" ", $triangle[$count - 1]); $result = array(); for ($i = 0; $i < count($up); $i++) { $item = array(); for ($j = $i; $j < $i + 2; $j++) { $item[] = $up[$i] + $down[$j]; } $result[] = max($item); } unset($triangle[$count - 1]); $triangle[$count - 2] = implode(" ", $result); return euler($triangle); }
public function benchEuler($params) { euler($params['x'], $params['y']); }
<?php $inputFile = fopen("project_euler_1.txt", "r") or die("Unable to open file!"); $data = array(); while (!feof($inputFile)) { $data[] = trim(fgets($inputFile)); } fclose($inputFile); $n = $data[0]; for ($i = 1; $i < $n + 1; $i++) { echo euler($data[$i], 3) + euler($data[$i], 5) - euler($data[$i], 15), "\n"; } function euler($k, $n) { $m = intval(($k - 1) / $n); return $n * $m * ($m + 1) / 2; }