Simple Collatz Conjecture (terminal/command prompt)

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Simple Collatz Conjecture (terminal/command prompt)

Postby mckryall » Sat Feb 08, 2014 7:57 am

Code: Select all
import time
#-----
def odd_even(n):
   return n % 2 == 0

def collatz(n):
   o_e = odd_even(n)
   if o_e == True:
      n = n / 2
   elif o_e == False:
      n = n * 3 + 1
   print n
   return n
def collatzloop(n):
   while n != 1:
      n = collatz(n)
def mainloop():
   n = int(raw_input("What number do you want to start with?\n> "))
   if n == 0:
      print "You broke it, you know that?"
      time.sleep(0.7)
      print "Now, just sit back and wait for the singularity."
      time.sleep(1)
      exit()
   while True:
      while n != 1:
         n = collatz(n)
         time.sleep(0.2)
      break
#-----
print "Collatz Conjecture Calculator"
print "Version 1.0"
print "Jan 12 2014\n"
time.sleep(1)
while True:
   try:
      mainloop()
      while True:
         again = raw_input("Do you want to do this again?\n> ")
         if again.lower() in ['y', 'yes']:
            break
         elif again.lower() in ['n', 'no']:
            exit()
         else:
            print "What? Try \"yes\" or \"no\""
   except KeyboardInterrupt:
      break
exit()

Last edited by Mekire on Sat Feb 08, 2014 8:00 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 3 guests