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 *

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
504