Let's converge to 9!
Perl 6, 41 bytes (40 chars)
{+($_,{$_%2??[+] $_².comb!!$_/2+1}...9)}
Try it online!
This uses 1-indexing of k's, so it gives 1 higher answers than the examples in OP. If this is not what the 1-indexing means, I'll have to add 1 byte more.
Explanation: It's an anonymous function. We just use the Perl 6's facility for generating lists using recursion :—). It looks like this: (first element),(block that takes the previous element and gives the next)...(end condition)
. In this case, the first element is $_
(argument of the main function) and the end condition is 9
(fulfilled when we generate a 9). In the middle block, we use $_
to refer to its argument ( = the previous element of the sequence). The ?? !!
is the old ternary operator (better known as ? :
). Finally, we take the length of this list by forcing numerical context by +(...)
.
The last weird thing here is the sum of digits. Numbers are Cool
(behave both like strings and numbers), so we use a string method .comb
on $_²
(give list of characters = digits), then adding the characters up (that converts them back to numbers).
Python 2, 129 126 76 68 67 64 54 53 bytes
-3 bytes thanks to Jonathan Frech. -8 bytes thanks to Maltysen. -7 bytes thanks to Jonathan Allan. -1 byte thanks to Mr. Xcoder.
f=lambda n:n-9and-~f(n%2*sum(map(int,`n*n`))or 1+n/2)
Try it online!
From somebody who probably doesn't know enough math, this seems completely arbitrary. :P
Jelly, 17 bytes
²DSµH‘$Ḃ?ßµ-n9$?‘
Try it online!
Straight-forward approach. Uses 0-based indexing.
Explanation
²DSµH‘$Ḃ?ßµ-n9$?‘ Input: n
? If
n9$ n != 9
µ Then
? If
Ḃ n % 2 == 1
µ Then
² Square
D Decimal digits
S Sum
$ Else
H Halve
‘ Increment
ß Call recursively
Else
- The constant -1
‘ Increment