Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from sys import argv
- from time import time
- usage = f'usage: {argv[0]} <number>'
- if len(argv) != 2:
- print(usage)
- quit(1)
- def is_even(num):
- if str(num)[-1] in ['0', '2', '4', '6', '8']:
- return True
- else:
- return False
- try:
- num = int(argv[1])
- except ValueError:
- print(usage)
- quit(1)
- else:
- count = 0
- t1 = time()
- while num != 1:
- if is_even(num):
- num = int(num / 2)
- elif not is_even(num):
- num = 3 * num + 1
- count += 1
- t2 = time()
- print(f'program took {t2-t1} seconds to run {count} iterations')
Add Comment
Please, Sign In to add comment