Skip to content

Instantly share code, notes, and snippets.

@ymotongpoo
Last active July 7, 2021 15:50
Show Gist options
  • Save ymotongpoo/b185aaef169f22af03296046e2571023 to your computer and use it in GitHub Desktop.
Save ymotongpoo/b185aaef169f22af03296046e2571023 to your computer and use it in GitHub Desktop.
コラッツ予想を1千万まで回してみた
#!/bin/env/python3
def even(x: int) -> int:
return x // 2
def odd(x: int) -> int:
return 3 * x + 1
def collatz(memo: set[int], x: int) -> None:
original = x
while x != 1:
if x in memo:
break
memo.add(x)
if x % 2 == 0:
x = even(x)
else:
x = odd(x)
print(f"{original} meets collatz problem")
def main() -> None:
memo = set()
i = 0
while i < 10_000_000:
collatz(memo, i)
i+=1
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment