Recursive Collatz Conjecture
Python 2, 70 bytes
f=lambda n,k=0,j=0:n-1and-~f(k*[n/2,n*3+1][n%2]or f(j/99or n,1),k,j+1)
Returns 199 for one hundred or more iterations.
Try it online!
Attache, 40 bytes
`-&3@`#@PeriodicSteps[CollatzSize@Max&1]
Try it online!
This is a new language that I made. I wanted to get around to making a proper infix language, and this is the result: a mathematica knock-off. Hooray?
Explanation
This is a composition of a few functions. These functions are:
PeriodicSteps[CollatzSize@Max&1]
This yields a function which applies its argument until the results contain a duplicate element. This function,CollatzSize@Max&1
, is applyingCollatzSize
to the greater of the input and1
, to avoid the invalid input0
to CollatSize.`#
is a quoted operator; when applied monadically in this sense, it obtains the size of its argument`-&3
is a bonded function, which bonds the argument3
to the function`-
, which reads as "minus 3". This is because the PeriodicSteps application yields0
s, which need to be accounted for. (It also neatly handles out-of-bounds numbers like5
, which map to-1
.)
C (gcc), 75 bytes
i,j;f(n){for(j=0;(i=n)&&j++<100;)for(n=0;i-1;++n)i=i&1?i*3+1:i/2;i=!i*j-1;}
Returns -1
for n>=100
iterations.
Try it online!
C (gcc), 73 bytes
i,j;f(n){for(j=-1;(i=n)&&j++<99;)for(n=0;i-1;++n)i=i&1?i*3+1:i/2;i=!i*j;}
Returns 0
for n>=100
iterations.
Try it online!