The Collatz conjecture
what i am trying to do:
Write a function called collatz_sequence that takes a starting integer and returns the sequence of integers, including the starting point, for that number. Return the sequence in the form of a list. Create your function so that if the user inputs any integer less than 1, it returns the empty list .
background on collatz conjecture:
Take any natural number n. If n is even, divide it by 2 to get n / 2, if n is odd multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
What I have so far:
seq = [x]
if x < 1:
while x > 1:
if x % 2 == 0:
You forgot to append the
x values to the
def collatz_sequence(x): seq = [x] if x < 1: return  while x > 1: if x % 2 == 0: x = x / 2 else: x = 3 * x + 1 seq.append(x) # Added line return seq
~/tmp$ python collatz.py [6, 3, 10, 5, 16, 8, 4, 2, 1]