This Python function outputs a list based on the collatz conjecture. This is an unsolved maths problem where the function will perform different operations on 'n' depending on if it is odd or even, outputting 'n' to a list called 'seq' each time the function repeats. Eventually 'n' will decay to the end point ('1') once the number '16' appears in the 'chain'.
I am trying to make the code as concise as possible here. Is there any way to shorten the function?
This is my newbie Python code:
def collatz(n):
seq = []
n = int(n)
if n == 0:
return
elif n == 1:
return seq + [n]
elif n > 1 == True and n % 2 == 0:
return seq + [n] + collatz(n/2)
else:
return seq + [n] + collatz(3*n+1)
print(collatz(7))
this outputs
[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]