Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # http://stackoverflow.com/questions/43922426/how-to-write-code-to-find-the-n-th-number-in-the-collatz-sequance
- # The following is a direct implementation based on what is shown here:
- # https://en.wikipedia.org/wiki/Collatz_conjecture#Statement_of_the_problem
- def f(a):
- if a % 2 == 0:
- return a//2
- else:
- return 3*a + 1
- def collatz(a, n):
- for _ in range(n):
- a = f(a)
- return a
- print(collatz(11, 5)) # -> 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement