Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. def hailstone(p):
  2. """Takes input and continues to put in output if even does p//2 if odd 3p+1"""
  3. H = []
  4. while p != 1:
  5. H.append(p)
  6. if p > 0:
  7. if p%2 == 0:
  8. p = p//2
  9. elif p%2 == 1:
  10. p = 3*p+1
  11. else:
  12. return("Invalid Input")
  13. if p == 1:
  14. H.append(1)
  15. return(H)
  16.  
  17.  
  18. print("Problem 1")
  19. print(hailstone(1))
  20.  
  21. print("Problem 2")
  22. print(hailstone(27))
  23.  
  24. print("problem 3")
  25. maxLength = 0
  26. for n in range(1, 20001):
  27. l = hailstone(n)
  28. m = len(l)
  29. if m > maxLength:
  30. maxLength = m
  31. largestInteger = n
  32. print(largestInteger, maxLength)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement