Manafall Daily Quest

Hailstone Sequence Length

Function Name: hailstoneSequenceLength

Description

In mathematics, the hailstone sequence (or Collatz sequence) is generated by the following process: begin with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The sequence will eventually reach the number 1, regardless of which positive integer is chosen initially. Your task is to create a function that computes the length of the hailstone sequence starting with a given positive integer.

Requirements

  • The function should take a single integer parameter greater than 0.
  • If the input is not a positive integer, the function should return null or an appropriate error depending on the language used.
  • The function must iterate through the hailstone sequence and return the length of the sequence including the initial number and 1.
  • Avoid recursion due to possible stack overflow for large input values.
  • Optimize the function for readability and efficiency.

Examples

If the function is called with 6, the hailstone sequence is: 6, 3, 10, 5, 16, 8, 4, 2, 1. The function should return 9, since there are 9 terms in the sequence.If the function is called with 11, the sequence is: 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. The function should return 15.

Links

https://en.wikipedia.org/wiki/Collatz_conjecture

Matrix Spiral Copy

Sat Aug 10 2024

Create a function that takes a 2D array (matrix) as a parameter and returns a list of its elements in spiral order.Spiral order starts at the top-left corner of the 2D array, goes to the right, and proceeds in a spiral pattern all the way until every element has been visited.

Prev Quest

Simple String Compression

Mon Aug 12 2024

You are tasked with writing a function that applies a basic form of string compression. The compression algorithm you should implement will take a string consisting solely of alphabetical characters and output a compressed version of the string. In the compressed version, sequences of repeated characters should be replaced by that character followed by an integer indicating how many times the character repeats consecutively. For instance, 'aaabcc' would be compressed to 'a3b1c2'. The function should only compress the string if it results in a smaller string than the input.

Next Quest