learningpython/collatz/collatzSequence.py

24 lines
435 B
Python
Executable file

#! /usr/bin/env python3
import sys
import collatz
usage='usage: collatzSequence [number] prints the collatz sequence for the given number'
if len(sys.argv)<2:
Print(usage)
sys.exit()
number = sys.argv[1]
try:
number = int(number)
except:
print(usage)
sys.exit()
seqLength = 0
while number != 1:
number = collatz.nextInSequence(number)
seqLength += 1
print(number)
print('length of '+str(seqLength))