Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. # Collatz Operation
  2. def collatz(number):
  3. if number % 2 == 0: # Check for Even
  4. operation = number // 2
  5. print(operation)
  6. return operation
  7. else:
  8. operation = 3 * number + 1
  9. print(operation)
  10. return operation
  11.  
  12.  
  13. # Recurse Collatz
  14. def callCollatz():
  15. try:
  16. num = int(input("Enter an integer: \n")) # Accept User Input and Parse it to Integer Type
  17. while True: # Infinite Loop
  18. num = collatz(num) # Set the Number To the Current Return Value of Collatz Operation in Local Iteration
  19. if num == 1: # Break If The Collatz Sequence Ends
  20. break
  21. except ValueError:
  22. print("Please input an integer!")
  23.  
  24.  
  25. # Invoke Collatz
  26. callCollatz()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement