2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3881000cf542c50fe9b
|
|
|
|
|
title: 'Problem 28: Number spiral diagonals'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 301930
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-28-number-spiral-diagonals
|
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
|
|
|
|
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
<div style='padding-left: 4em;'>
|
|
|
|
|
<div style='color: red; display: inline;'>21</div> 22 23 24 <div style='color: red; display: inline;'>25</div><br>
|
|
|
|
|
20 <div style='color: red; display: inline;'>7</div> 8 <div style='color: red; display: inline;'>9</div> 10<br>
|
|
|
|
|
19 6 <div style='color: red; display: inline;'>1</div> 2 11<br>
|
|
|
|
|
18 <div style='color: red; display: inline;'>5</div> 4 <div style='color: red; display: inline;'>3</div> 12<br>
|
|
|
|
|
<div style='color: red; display: inline;'>17</div> 16 15 14 <div style='color: red; display: inline;'>13</div><br>
|
|
|
|
|
</div>
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
It can be verified that the sum of the numbers on the diagonals is 101.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
What is the sum of the numbers on the diagonals in an `n` by `n` spiral formed in the same way?
|
|
|
|
|
|
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
|
|
|
|
`spiralDiagonals(101)` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof spiralDiagonals(101) === 'number');
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`spiralDiagonals(101)` should return 692101.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(spiralDiagonals(101) == 692101);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`spiralDiagonals(303)` should return 18591725.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(spiralDiagonals(303) == 18591725);
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`spiralDiagonals(505)` should return 85986601.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-11-27 18:02:05 +00:00
|
|
|
|
assert(spiralDiagonals(505) == 85986601);
|
|
|
|
|
```
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`spiralDiagonals(1001)` should return 669171001.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(spiralDiagonals(1001) == 669171001);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --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
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
function spiralDiagonals(n) {
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
return n;
|
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
spiralDiagonals(1001);
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
```
|