Advertisement
Locoluis

Syracuse / Collatz

Oct 29th, 2013
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.27 KB | None | 0 0
  1. import re
  2.  
  3. rn = re.compile(r'^[0-9]+$')
  4.  
  5. num = raw_input("Enter a natural number: ")
  6. while not rn.match(num):
  7.     num = raw_input("Enter a natural number: ")
  8. num = int(num)
  9.  
  10. while num != 1:
  11.     print num
  12.     if (num % 2):
  13.         num = 3 * num + 1
  14.     else:
  15.         num /= 2
  16. print num
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement