<?php
/**
* Project Euler Problem 1: Multiples of 3 and 5
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we
* get 3, 5, 6 and 9. The sum of these multiples is 23.
*
* Find the sum of all the multiples of 3 or 5 below 1000.
*
* Problem: https://p...content-available-to-author-only...r.net/problem=1
* Solution: https://g...content-available-to-author-only...b.com/potherca-blog/ProjectEuler/blob/master/src/PHP/Solutions/Problem001.php
* Live code: https://i...content-available-to-author-only...e.com/ViI6sA
*/
namespace Potherca\ProjectEuler\Solutions\Problem001
{
use Potherca\ProjectEuler\Calculators\MultiplesCalculator as Calculator;
$multiples = [3, 5];
$limit = 1000;
$solutions = (new Calculator($multiples))->getSumOfMultiplesBelow($limit);
echo $solutions;
}
namespace Potherca\ProjectEuler\Calculators
{
class MultiplesCalculator
{
private $multiples;
final
public function __construct
(array $multiples) {
$this->multiples = array_map(function ($multiple) { return (int) $multiple;
}, $multiples);
}
final
public function getMultiplesBelow
(int
$limit): array {
$multiples = [];
for ($counter = 0 ; $counter < $limit; $counter++) {
if ($this->isMultipleOf($this->multiples, $counter)) {
$multiples[] = $counter;
}
}
return $multiples;
}
final public function getSumOfMultiplesBelow(int $limit): int
{
return array_sum($this->getMultiplesBelow($limit)); }
private function isMultipleOf
(array $targets, int
$subject): bool
{
return $subject % $target === 0;
}));
}
}
}
PD9waHAKCi8qKgogKiBQcm9qZWN0IEV1bGVyIFByb2JsZW0gMTogTXVsdGlwbGVzIG9mIDMgYW5kIDUKICogCiAqIElmIHdlIGxpc3QgYWxsIHRoZSBuYXR1cmFsIG51bWJlcnMgYmVsb3cgMTAgdGhhdCBhcmUgbXVsdGlwbGVzIG9mIDMgb3IgNSwgd2UKICogZ2V0IDMsIDUsIDYgYW5kIDkuIFRoZSBzdW0gb2YgdGhlc2UgbXVsdGlwbGVzIGlzIDIzLgogKiAKICogRmluZCB0aGUgc3VtIG9mIGFsbCB0aGUgbXVsdGlwbGVzIG9mIDMgb3IgNSBiZWxvdyAxMDAwLgogKiAKICogUH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ICAgICAgICAgICAgICAgIHJldHVybiAoaW50KSAkbXVsdGlwbGU7CiAgICAgICAgICAgIH0sICRtdWx0aXBsZXMpOwogICAgICAgIH0KCiAgICAgICAgZmluYWwgcHVibGljIGZ1bmN0aW9uIGdldE11bHRpcGxlc0JlbG93KGludCAkbGltaXQpOiBhcnJheQogICAgICAgIHsKICAgICAgICAJJG11bHRpcGxlcyA9IFtdOwoKICAgICAgICAgICAgZm9yICgkY291bnRlciA9IDAgOyAkY291bnRlciA8ICRsaW1pdDsgJGNvdW50ZXIrKykgewogICAgICAgICAgICAgICAgaWYgKCR0aGlzLT5pc011bHRpcGxlT2YoJHRoaXMtPm11bHRpcGxlcywgJGNvdW50ZXIpKSB7CiAgICAgICAgICAgICAgICAgICAgJG11bHRpcGxlc1tdID0gJGNvdW50ZXI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIHJldHVybiAkbXVsdGlwbGVzOwogICAgICAgIH0KCiAgICAgICAgZmluYWwgcHVibGljIGZ1bmN0aW9uIGdldFN1bU9mTXVsdGlwbGVzQmVsb3coaW50ICRsaW1pdCk6IGludAogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGFycmF5X3N1bSgkdGhpcy0+Z2V0TXVsdGlwbGVzQmVsb3coJGxpbWl0KSk7CiAgICAgICAgfQoKICAgICAgICBwcml2YXRlIGZ1bmN0aW9uIGlzTXVsdGlwbGVPZihhcnJheSAkdGFyZ2V0cywgaW50ICRzdWJqZWN0KTogYm9vbAogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIChib29sKSBjb3VudChhcnJheV9maWx0ZXIoJHRhcmdldHMsIGZ1bmN0aW9uICgkdGFyZ2V0KSB1c2UgKCRzdWJqZWN0KSB7CiAgICAgICAgICAgICAgICByZXR1cm4gJHN1YmplY3QgJSAkdGFyZ2V0ID09PSAwOwogICAgICAgICAgICB9KSk7CiAgICAgICAgfQogICAgfQp9Cg==