2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								title: Hailstone sequence
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								id: 595608ff8bcd7a50bd490181
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								forumTopicId: 302279
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Description
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'description' > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The Hailstone sequence of numbers can be generated from a starting positive integer, < code > n< / code >  by:
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< ul > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < li > If < code > n< / code >  is < code > 1< / code >  then the sequence ends< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  < li > If < code > n< / code >  is < code > even< / code >  then the next < code > n< / code >  of the sequence < code > = n/2< / code > < / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  < li > If < code > n< / code >  is < code > odd< / code >  then the next < code > n< / code >  of the sequence < code > = (3 * n) + 1< / code > < / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< / ul > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The (unproven) < a  href = "https://en.wikipedia.org/wiki/Collatz conjecture"  title = "wp: Collatz conjecture"  target = "_blank" > Collatz conjecture< / a >  is that the hailstone sequence for any starting number always terminates.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Instructions
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'instructions' > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< ol > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < li > Create a routine to generate the hailstone sequence for a number< / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < li > Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with < code > 27, 82, 41, 124< / code >  and ending with < code > 8, 4, 2, 1< / code > < / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  < li > Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length. (But don't show the actual sequence!)< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / ol > 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< strong > See also:< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< ul > 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-22 23:30:29 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  < li > < a  href = "https://xkcd.com/710"  target = "_blank" > xkcd< / a >  (humourous).< / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								< / ul > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Tests
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'tests' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  -  text: < code > hailstoneSequence</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert(typeof hailstoneSequence === 'function');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  text: < code > hailstoneSequence()</ code >  should return < code > [[27,82,41,124,8,4,2,1], [351, 77031]]</ code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    testString: assert.deepEqual(hailstoneSequence(), res);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Challenge Seed
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'challengeSeed' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< div  id = 'js-seed' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// noprotect
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 18:37:06 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								function hailstoneSequence() {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								  const res = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  // Good luck!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  return res;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### After Test
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< div  id = 'js-teardown' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								const res = [[27, 82, 41, 124, 8, 4, 2, 1], [351, 77031]];
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Solution
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< section  id = 'solution' > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// noprotect
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function hailstoneSequence () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  const res = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  function hailstone(n) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    const seq = [n];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    while (n > 1) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      n = n % 2 ? 3 * n + 1 : n / 2;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      seq.push(n);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return seq;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  const h = hailstone(27);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  const hLen = h.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  res.push([...h.slice(0, 4), ...h.slice(hLen - 4, hLen)]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  let n = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  let max = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  for (let i = 100000; --i;) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    const seq = hailstone(i);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    const sLen = seq.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (sLen > max) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      n = i;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      max = sLen;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  res.push([max, n]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  return res;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								< / section >