Guest User

Untitled

a guest
Nov 16th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from sys import argv
  3. from time import time
  4.  
  5. usage = f'usage: {argv[0]} <number>'
  6.  
  7. if len(argv) != 2:
  8. print(usage)
  9. quit(1)
  10.  
  11.  
  12. def is_even(num):
  13. if str(num)[-1] in ['0', '2', '4', '6', '8']:
  14. return True
  15. else:
  16. return False
  17.  
  18.  
  19. try:
  20. num = int(argv[1])
  21. except ValueError:
  22. print(usage)
  23. quit(1)
  24. else:
  25. count = 0
  26. t1 = time()
  27. while num != 1:
  28. if is_even(num):
  29. num = int(num / 2)
  30. elif not is_even(num):
  31. num = 3 * num + 1
  32. count += 1
  33. t2 = time()
  34. print(f'program took {t2-t1} seconds to run {count} iterations')
Add Comment
Please, Sign In to add comment