Vyxal s
, 6 bytes
≬K¯tẋ±
Ties with Steffan's answer, but uses the algorithm defined by the challenge.
This is the same as the 7 byte answer below (so please see below for its explanation), except its final operation is ċ
instead of L‹
:
± # Get the sign of a number
Then the s
flag sums the resulting list, which contains all 1s except for one 0, thus yielding the length of the list minus 1.
Vyxal l
, 6 bytes
≬K¯tẋṪ
This is the same as the 7 byte answer below except its final operation is Ṫ
instead of L‹
:
Ṫ # Tail Remove - Return all but the last item of a list
Then the l
flag takes the length of that list.
Vyxal, 7 bytes
≬K÷εẋL‹
≬ # 3-element lambda:
K # Push a list of the divisors, from 1 to the number itself in increasing order
÷ # Unwrap the list onto the stack
ε # Absolute difference of the last two items (this will be the largest two)
ẋ # Repeat the lambda on the number at the top of the stack (which is initially the
# input) until the result no longer changes, returning a list of the results. The
# last result will be zero, because the list of divisors of the penultimate result,
# 1, is just ⟨1⟩, but ε will subtract that 1 from itself.
L # Length
‹ # Decrement - subtract 1
-
also works in place of ε
, i.e. ≬K÷-ẋL‹
. The sign of the intermediate result keeps flipping, since the subtraction is done in the "wrong" direction, but the number of steps to get to zero is the same.
This is very slow for most numbers of more than 60 digits or so, since it has to generate a full list of divisors (even if it only uses the largest two). Prime factorization is still fast enough at that level, but unless the number only has one distinct prime factor, the list of divisors will be orders of magnitude longer.
Alternative 7 byter:
≬K¯tẋL‹
Only the lambda is different in this:
K # Push a list of the divisors, from 1 to the number itself in increasing order
¯ # Deltas (consecutive differences)
t # Tail - get the last item of the list
This still takes one more step than we want, requiring the ‹
decrement, because t
tail returns zero for an empty list. And even if it returned something else (such as empty list), the fixed point would still be that result, and not 1, thus still requiring the decrement.