Collatz conjecture tester


/ Published in: Python
Save to your folder(s)

Cool little code that will test the Collatz conjecture


Copy this code and paste it in your HTML
  1. def mathprob_no_print(x):
  2. while x != 1:
  3. if x % 2 == 0:
  4. x = x/2
  5. else:
  6. x = 3 * x + 1
  7. if x == 1:
  8. return x
  9.  
  10.  
  11. def mathprob(x):
  12. string = str(x)
  13. print string+"!!!!!!!!!!!!!!!!! This is the starting number!"
  14. while x != 1:
  15. if x % 2 == 0:
  16. x = x/2
  17. print x
  18. else:
  19. x = 3 * x + 1
  20. print x
  21. if x == 1:
  22. return x
  23.  
  24. def math_repeat(x,p):
  25. while x <= p:
  26. mathprob (x)
  27. x = x + 1
  28. if mathprob_no_print(x) != 1:
  29. print "****YAY****YAY****WE PROVED HIS CONJECTURE WRONG!!!!!!!!!!"
  30. math_repeat(1,100)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.