* fix: improve Project Euler descriptions and test case Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54 * feat: add typeof tests and gave functions proper names for first 100 challenges * fix: continue fixing test descriptions and adding "before test" sections * fix: address review comments * fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays * fix: implement bug fixes and improvements from review * fix: remove console.log statements from seed and solution
2.2 KiB
2.2 KiB
id, challengeType, title, forumTopicId
id | challengeType | title | forumTopicId |
---|---|---|---|
5900f3881000cf542c50fe9b | 5 | Problem 28: Number spiral diagonals | 301930 |
Description
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
21
22 23 24 25
20
7
8 9
1019 6
1
2 1118
5
4 3
1217
16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in an n
by n
spiral formed in the same way?
Instructions
Tests
tests:
- text: <code>spiralDiagonals(101)</code> should return a number.
testString: assert(typeof spiralDiagonals(101) === 'number');
- 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;
};