Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Collatz Ztalloc by Magu
def collatz_convert(data: int | str) -> int | str | None:
n = 1; st = ''
if type(data) is str:
for s in data[::-1]:
if s == 'd':
n *= 2
else:
n, r = divmod(n - 1, 3)
if r or not (n % 2):
return None
return n
else:
while True:
if not (data % 2):
data //= 2; st += 'd'
if data < 2:
break
else:
data = data * 3 + 1; st += 'u'
return st
Sept. 21, 2024