<?php
/**
* Project Euler Problem 2: Even Fibonacci numbers
*
* Each new term in the Fibonacci sequence is generated by adding the previous
* two terms. By starting with 1 and 2, the first 10 terms will be:
*
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* The sum of the even-valued terms whose values do not exceed one hundred would
* be 44 (2 + 8 + 34)
*
* By considering the terms in the Fibonacci sequence whose values do not exceed
* four million, find the sum of the even-valued terms.
*
* Problem: https://p...content-available-to-author-only...r.net/problem=2
* Solution: https://g...content-available-to-author-only...b.com/potherca-blog/ProjectEuler/blob/master/src/PHP/Solutions/Problem002.php
* Live code: https://i...content-available-to-author-only...e.com/Sur5V4
*/
namespace Potherca\ProjectEuler\Solutions\Problem002
{
use Potherca\ProjectEuler\Calculators\FibonacciCalculator as Calculator;
$limit = 4000000;
$solution = (new Calculator())->getSumOfEvenTermsBelow($limit);
echo $solution;
}
namespace Potherca\ProjectEuler\Calculators
{
class FibonacciCalculator
{
final public function getSumOfEvenTermsBelow(int $limit): int
{
return array_sum($this->getEvenTermsBelow($limit)); }
final
public function getEvenTermsBelow
(int
$limit): array {
return array_filter($this->getTermsBelow($limit), function ($term) { return $term % 2 === 0;
});
}
final
public function getTermsBelow
(int
$limit): array {
$terms = [];
$previousTerm = 0;
$currentTerm = 1;
while ($previousTerm + $currentTerm < $limit) {
$term = $previousTerm + $currentTerm;
$terms[] = $term;
$previousTerm = $currentTerm;
$currentTerm = $term;
};
return $terms;
}
}
}
PD9waHAKCi8qKgogKiBQcm9qZWN0IEV1bGVyIFByb2JsZW0gMjogRXZlbiBGaWJvbmFjY2kgbnVtYmVycwogKiAKICogRWFjaCBuZXcgdGVybSBpbiB0aGUgRmlib25hY2NpIHNlcXVlbmNlIGlzIGdlbmVyYXRlZCBieSBhZGRpbmcgdGhlIHByZXZpb3VzCiAqIHR3byB0ZXJtcy4gQnkgc3RhcnRpbmcgd2l0aCAxIGFuZCAyLCB0aGUgZmlyc3QgMTAgdGVybXMgd2lsbCBiZToKICogCiAqCQk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AgICAKICAgIGVjaG8gJHNvbHV0aW9uOwp9CgpuYW1lc3BhY2UgUG90aGVyY2FcUHJvamVjdEV1bGVyXENhbGN1bGF0b3JzCnsKICAgIGNsYXNzIEZpYm9uYWNjaUNhbGN1bGF0b3IKICAgIHsKICAgICAgICBmaW5hbCBwdWJsaWMgZnVuY3Rpb24gZ2V0U3VtT2ZFdmVuVGVybXNCZWxvdyhpbnQgJGxpbWl0KTogaW50CiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gYXJyYXlfc3VtKCR0aGlzLT5nZXRFdmVuVGVybXNCZWxvdygkbGltaXQpKTsKICAgICAgICB9CgogICAgICAgIGZpbmFsIHB1YmxpYyBmdW5jdGlvbiBnZXRFdmVuVGVybXNCZWxvdyhpbnQgJGxpbWl0KTogYXJyYXkKICAgICAgICB7CiAgICAgICAgCXJldHVybiBhcnJheV9maWx0ZXIoJHRoaXMtPmdldFRlcm1zQmVsb3coJGxpbWl0KSwgZnVuY3Rpb24gKCR0ZXJtKSB7CiAgICAgICAgCQlyZXR1cm4gJHRlcm0gJSAyID09PSAwOwogICAgICAgIAl9KTsKICAgICAgICB9CgogICAgICAgIGZpbmFsIHB1YmxpYyBmdW5jdGlvbiBnZXRUZXJtc0JlbG93KGludCAkbGltaXQpOiBhcnJheQogICAgICAgIHsKICAgICAgICAJJHRlcm1zID0gW107CgogICAgICAgIAkkcHJldmlvdXNUZXJtID0gMDsKCQkJJGN1cnJlbnRUZXJtID0gMTsKCiAgICAgICAgCXdoaWxlICgkcHJldmlvdXNUZXJtICsgJGN1cnJlbnRUZXJtIDwgJGxpbWl0KSB7CgkgICAgICAgIAkkdGVybSA9ICRwcmV2aW91c1Rlcm0gKyAkY3VycmVudFRlcm07CgogICAgICAgIAkJJHRlcm1zW10gPSAkdGVybTsKICAgICAgICAJCQogICAgICAgIAkJJHByZXZpb3VzVGVybSA9ICRjdXJyZW50VGVybTsKICAgICAgICAJCSRjdXJyZW50VGVybSA9ICR0ZXJtOwogICAgICAgIAl9OwogICAgICAgIAkKICAgICAgICAJcmV0dXJuICR0ZXJtczsKICAgICAgICB9CiAgICB9Cn0K