Last active
November 17, 2018 17:03
-
-
Save mentix02/0e4ffeee7ccae2bbd02b84e5224fb68a to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/python3 | |
| from time import time | |
| from argparse import ArgumentParser | |
| __version__ = '1.0.0' | |
| parser = ArgumentParser(description='a simple collatz sequence solver') | |
| parser.add_argument('n', help='number', action='store', type=int) | |
| parser.add_argument('-t', '--time', help='show time taken', action='store_true') | |
| parser.add_argument('-V', '--verbose', help='verbose mode', action='store_true') | |
| parser.add_argument('-v', '--version', help='display version', action='version', version=f'%(prog)s {__version__}') | |
| args = parser.parse_args() | |
| def is_even(num: int): | |
| if str(num)[-1] in ['0', '2', '4', '6', '8']: | |
| return True | |
| return False | |
| num = args.n | |
| if args.time: | |
| t1 = time() | |
| counter = 0 | |
| while num != 1: | |
| if args.verbose: | |
| print(num, end=' ') | |
| if is_even(num): | |
| num //= 2 | |
| else: | |
| num = (num * 3) + 1 | |
| if args.time: | |
| counter += 1 | |
| print(1) if args.verbose else print('', end='') | |
| if args.time: | |
| t2 = time() | |
| print(f'completed' + (f' in {t2-t1} after {counter} iterations' if args.time else '')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment