2.1 KiB
2.1 KiB
id, challengeType, title, forumTopicId, localeTitle
id | challengeType | title | forumTopicId | localeTitle |
---|---|---|---|---|
5900f3721000cf542c50fe85 | 5 | Problem 6: Sum square difference | 302171 | Задача 6: Суммарный квадрат |
Description
1 2 + 2 2 + ... + 10 2 = 385
Квадрат суммы первых десяти натуральных чисел есть, (1 + 2 + ... + 10) 2 = 55 2 = 3025
Следовательно, разница между суммой квадратов первых десяти натуральных чисел и квадратом суммы равна 3025 - 385 = 2640. Найдите разницу между суммой квадратов первых n
натуральных чисел и квадратом суммы.
Instructions
Tests
tests:
- text: <code>sumSquareDifference(10)</code> should return 2640.
testString: assert.strictEqual(sumSquareDifference(10), 2640);
- text: <code>sumSquareDifference(20)</code> should return 41230.
testString: assert.strictEqual(sumSquareDifference(20), 41230);
- text: <code>sumSquareDifference(100)</code> should return 25164150.
testString: assert.strictEqual(sumSquareDifference(100), 25164150);
Challenge Seed
function sumSquareDifference(n) {
// Good luck!
return true;
}
sumSquareDifference(100);
Solution
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;
}