1.6 KiB
1.6 KiB
id | challengeType | title | forumTopicId |
---|---|---|---|
5900f3881000cf542c50fe9b | 5 | Problem 28: Number spiral diagonals | 301930 |
Description
Instructions
Tests
tests:
- text: <code>spiralDiagonals(101)</code> should return 692101.
testString: assert(spiralDiagonals(101) == 692101);
- text: <code>spiralDiagonals(303)</code> should return 18591725.
testString: assert(spiralDiagonals(303) == 18591725);
- text: <code>spiralDiagonals(505)</code> should return 85986601.
testString: assert(spiralDiagonals(505) == 85986601);
- text: <code>spiralDiagonals(1001)</code> should return 669171001.
testString: assert(spiralDiagonals(1001) == 669171001);
Challenge Seed
function spiralDiagonals(n) {
// Good luck!
return n;
}
spiralDiagonals(1001);
Solution
const spiralDiagonals = (n) => {
const Sn2 = (n) => {
return n*(n+1)*(2*n+1)/6;
};
const Sn = (n) => {
return n*(n+1)/2;
};
let sum = (Sn2(n-1) + Sn(n-1) + n-1) + (Math.floor(n/2) + Sn2(n));
return sum;
};