2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3721000cf542c50fe85
|
|
|
|
|
title: 'Problem 6: Sum square difference'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 302171
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-6-sum-square-difference
|
2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --description--
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
The sum of the squares of the first ten natural numbers is,
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
<div style='text-align: center;'>1<sup>2</sup> + 2<sup>2</sup> + ... + 10<sup>2</sup> = 385</div>
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
The square of the sum of the first ten natural numbers is,
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
<div style='text-align: center;'>(1 + 2 + ... + 10)<sup>2</sup> = 55<sup>2</sup> = 3025</div>
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
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.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
Find the difference between the sum of the squares of the first `n` natural numbers and the square of the sum.
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`sumSquareDifference(10)` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof sumSquareDifference(10) === 'number');
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`sumSquareDifference(10)` should return 2640.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(sumSquareDifference(10), 2640);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`sumSquareDifference(20)` should return 41230.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(sumSquareDifference(20), 41230);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`sumSquareDifference(100)` should return 25164150.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(sumSquareDifference(100), 25164150);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function sumSquareDifference(n) {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sumSquareDifference(100);
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const sumSquareDifference = (number)=>{
|
|
|
|
|
let squareOfSum = Math.pow(sumOfArithmeticSeries(1,1,number),2);
|
|
|
|
|
let sumOfSquare = sumOfSquareOfNumbers(number);
|
|
|
|
|
return squareOfSum - sumOfSquare;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sumOfArithmeticSeries(a,d,n){
|
|
|
|
|
return (n/2)*(2*a+(n-1)*d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sumOfSquareOfNumbers(n){
|
|
|
|
|
return (n*(n+1)*(2*n+1))/6;
|
|
|
|
|
}
|
|
|
|
|
```
|