<?php
/**
* Project Euler Problem 6: Sum square difference
*
* The sum of the squares of the first ten natural numbers is,
*
* 1² + 2² + ... + 10² = 385
*
* The square of the sum of the first ten natural numbers is,
*
* (1 + 2 + ... + 10)² = 55² = 3025
*
* Hence the difference between the sum of the squares of the first ten natural
* numbers and the square of the sum is 3025 − 385 = 2640.
*
* Find the difference between the sum of the squares of the first one hundred
* natural numbers and the square of the sum.
*
* Problem: https://p...content-available-to-author-only...r.net/problem=6
* Solution: https://g...content-available-to-author-only...b.com/potherca-blog/ProjectEuler/blob/master/src/PHP/Solutions/Problem006.php
* Live code: https://i...content-available-to-author-only...e.com/zgflJ5
*/
namespace Potherca\ProjectEuler\Solutions\Problem006
{
use Potherca\ProjectEuler\Calculators\SumSquareDifferenceCalculator as Calculator;
$limit = 100;
$solution = (new Calculator())->getSumSquareDifference($limit);
echo $solution;
}
namespace Potherca\ProjectEuler\Calculators
{
class SumSquareDifferenceCalculator
{
final public function getSumSquareDifference(int $limit): int
{
$range = range(1, $limit);
return $this->getSquareOfSums($range) - $this->getSumOfSquares($range);
}
private function getSquareOfSums
(array $range): int
{
}
private function getSumOfSquares
(array $range): int
{
return $number ** 2;
}, $range)
);
}
}
}
PD9waHAKCi8qKgogKiBQcm9qZWN0IEV1bGVyIFByb2JsZW0gNjogU3VtIHNxdWFyZSBkaWZmZXJlbmNlCiAqIAogKiBUaGUgc3VtIG9mIHRoZSBzcXVhcmVzIG9mIHRoZSBmaXJzdCB0ZW4gbmF0dXJhbCBudW1iZXJzIGlzLAogKiAKICoJCTHCsiArIDLCsiArIC4uLiArIDEwwrIgPSAzODUKICogCiAqIFRoZSBzcXVhcmUgb2YgdGhlIHN1bSBvZiB0aGUgZmlyc3QgdGVuIG5hdHVyYWwgbnVtYmVycyBpcywKICogCiAqCQkoMSArIDIgKyAuLi4gKyAxMCnCsiA9IDU1wrIgPSAzMDI1CiAqIAogKiBIZW5jZSB0aGUgZGlmZmVyZW5jZSBiZXR3ZWVuIHRoZSBzdW0gb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