Python program to generate Collatz Conjecture sequence.

# © 2017 TheFlyingKeyboard and released under MIT License
# theflyingkeyboard.net

def collatzConjecture(num):
  if num % 2 == 0:
    return num / 2
  else:
    return 3 * num + 1

number = int(input("Enter a number"))
iterNum = 0

if number > 0:
  while number != 1:
    number = collatzConjecture(number)
    print(number, end=' ')
    
    iterNum += 1
  
  print("\nIt took", iterNum, "iterations to reach 1")

 

Python Collatz Conjecture Sequence Generator
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *