Create a function to determine the length of a sequence of numbers obtained via the Collatz conjecture, ending at ::1::.




One of the simplest yet extremely complicated to prove concepts in mathematics is that of the Collatz conjecture, proposed in 1937 by Lothar Collatz.

It's known as a conjecture (contrary to a theorem) because over all these years of mathematical revolution, no one has been able to prove it. Yes, no one!

The Collatz conjecture can be described as follows:

Take any positive integer ::n::. If it's even, divide it by ::2:: to obtain the next integer. Or else, multiply by ::3:: and then add ::1:: to obtain the next integer. Continue this process.

It's widely believed that for any starting integer, the whole sequence of integers eventually reaches ::1::.

And this is the conjecture: that we'd eventually reach the integer ::1:: no matter which starting integer we use.

The Collatz conjecture has been tested for a humongous array of numbers. It has been shown that the conjecture holds for all positive integers approximately up to the value ::2^{68}:: (that's a huge huge huge number!).

Anyways, let's consider some examples.

Here's the sequence of integers starting at ::3::, up to the point we reach ::1:::


Here's the sequence of integers starting at ::17::, once again up to the point we reach ::1:::


Here's the sequence of integers starting at ::30:::


And you can try it for any other positive integer yourself! See how far does the sequence go.

Now consider the sequence of integers starting at a given positive integer ::n:: and ending at ::1::, whereby every subsequent element is obtained by performing the rules mentioned above. The length of this sequence is simply the total number of elements in the sequence.

For instance, the length of the sequence that begins at ::3:: is ::8:: (as there are a total of 8 elements in it). Similarly, the length of the sequence that begins at ::17:: is ::13::.

In this exercise, you have to create a function called collatz_length() that takes in the starting value of the sequence as an argument and returns back its length.

Note that if the provided argument is not a positive integer, the function must return NULL:

Here's an example of its usage:


function collatz_length( /* ... */ ) { /* ... */ }

echo 'collatz_length(3): ', collatz_length(3), "\n";
echo 'collatz_length(17): ', collatz_length(17), "\n";
echo 'collatz_length(30): ', collatz_length(30), "\n";
echo 'collatz_length(10100000): ', collatz_length(10100000), "\n";
echo 'collatz_length(false): ', collatz_length(false), "\n";
echo 'collatz_length(INF): ', collatz_length(INF), "\n";
echo 'collatz_length(2.3): ', collatz_length(2.3), "\n";
echo 'collatz_length(0): ', collatz_length(0), "\n";
collatz_length(3): 8 collatz_length(17): 13 collatz_length(30): 19 collatz_length(10100000): 133 collatz_length(false): collatz_length(INF): collatz_length(2.3): collatz_length(0):

The first three lines in the output represent the lengths of three sequences shown above, respectively.

The last four lines, however, all represent invalid values passed in to collatz_length(), to which the function returns NULL. Remember that printing NULL in PHP doesn't display anything.