Created
July 31, 2021 10:15
-
-
Save CTimmerman/a0555fa788b5bdcd749eb5be59b3421b to your computer and use it in GitHub Desktop.
Collatz conjecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Collatz conjecture. Inspired by https://www.youtube.com/watch?v=094y1Z2wpJg | |
2021-07-31 by Cees Timmerman | |
""" | |
def collatz(n: int) -> int: | |
return 3*n+1 if n%2 else n//2 | |
def collatz_count(start: int) -> int: | |
step = 0 | |
while True: | |
if start == 1: | |
return step | |
step += 1 | |
start = collatz(start) | |
if __name__ == "__main__": | |
# 1e309+ is float infinity on Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] so use 10**309+ integer which is also more accurate. | |
print(collatz_count(10**30000)) # 523,760 steps. Less than the minimum of 186,000,000,000 claimed on https://youtu.be/094y1Z2wpJg?t=660 which is enough proof for me. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment