2018-09-30 23:01:58 +01:00
---
id: 595608ff8bcd7a50bd490181
2020-11-27 19:02:05 +01:00
title: Hailstone sequence
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302279
2021-01-13 03:31:00 +01:00
dashedName: hailstone-sequence
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
The Hailstone sequence of numbers can be generated from a starting positive integer, `n` by:
2021-03-04 22:28:47 +01:00
- If `n` is `1` then the sequence ends
- If `n` is `even` then the next `n` of the sequence `= n/2`
- If `n` is `odd` then the next `n` of the sequence `= (3 * n) + 1`
2020-11-27 19:02:05 +01:00
2021-03-04 22:28:47 +01:00
The (unproven) Collatz conjecture is that the hailstone sequence for any starting number always terminates.
2020-11-27 19:02:05 +01:00
2019-03-05 18:37:06 +09:00
The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud), or as the Collatz sequence.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2021-03-04 22:28:47 +01:00
1. Create a routine to generate the hailstone sequence for a number
2. Your function should return an array with the number less than `limit` which has the longest hailstone sequence and that sequence's length. (But don't show the actual sequence!)
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`hailstoneSequence` should be a function.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof hailstoneSequence === 'function');
2018-09-30 23:01:58 +01:00
```
2021-03-04 22:28:47 +01:00
`hailstoneSequence(30)` should return an array.
2018-09-30 23:01:58 +01:00
```js
2021-03-04 22:28:47 +01:00
assert(Array.isArray(hailstoneSequence(30)));
2018-09-30 23:01:58 +01:00
```
2021-03-04 22:28:47 +01:00
`hailstoneSequence(30)` should return `[27, 112]` .
```js
assert.deepEqual(hailstoneSequence(30), [27, 112]);
```
`hailstoneSequence(50000)` should return `[35655, 324]` .
```js
assert.deepEqual(hailstoneSequence(50000), [35655, 324]);
```
2018-09-30 23:01:58 +01:00
2021-03-04 22:28:47 +01:00
`hailstoneSequence(100000)` should return `[77031, 351]` .
2018-09-30 23:01:58 +01:00
```js
2021-03-04 22:28:47 +01:00
assert.deepEqual(hailstoneSequence(100000), [77031, 351]);
2018-09-30 23:01:58 +01:00
```
2021-03-04 22:28:47 +01:00
# --seed--
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
2021-03-04 22:28:47 +01:00
function hailstoneSequence(limit) {
2020-11-27 19:02:05 +01:00
const res = [];
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
return res;
}
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-03-04 22:28:47 +01:00
function hailstoneSequence (limit) {
2018-09-30 23:01:58 +01:00
function hailstone(n) {
const seq = [n];
while (n > 1) {
n = n % 2 ? 3 * n + 1 : n / 2;
seq.push(n);
}
return seq;
}
let n = 0;
let max = 0;
2021-03-04 22:28:47 +01:00
for (let i = limit; --i;) {
2018-09-30 23:01:58 +01:00
const seq = hailstone(i);
const sLen = seq.length;
if (sLen > max) {
n = i;
max = sLen;
}
}
2021-03-04 22:28:47 +01:00
return [n, max];
2018-09-30 23:01:58 +01:00
}
```