Last active
August 1, 2017 02:52
-
-
Save toonarmycaptain/595da72bf36fc4ad8b4f580cf86d8e82 to your computer and use it in GitHub Desktop.
Collatz sequence function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python3 | |
# -*- coding: utf-8 -*- | |
# Collatz.py - a Collatz sequence program | |
""" | |
Created on Mon Jul 17 11:28:00 2017 | |
@author: david.antonini | |
Takes input from user and runs Collatz sequnce, printing each step | |
""" | |
def collatz(number): | |
if number % 2 == 0: | |
return number // 2 | |
else: | |
return number*3 + 1 | |
number = 0 | |
while number == 0: | |
try: | |
number = int(input('Please enter a number: ')) | |
if number == 0: | |
print('Number must be an integer not equal to zero.') | |
else: | |
while True: | |
number = collatz(number) | |
print(number) | |
if abs(number) == 1 or number == -5 or number == -17: | |
break | |
except ValueError: | |
print('Number must be an integer.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment