0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プログラマ脳を鍛える数学パズルQ06(改造版)コラッツの予想

Posted at

#問題概要
10000以下の偶数のうち、以下の操作を繰り返して自分自身に戻る数を見つける。

  • 初回は3を掛けて1を足す
  • 2回目以降は
    • 偶数の場合、nを2で割る
    • 奇数の場合、nに3を掛けて1を足す

Code

loopnums = []
for num in list(range(2, 10000, 2)):
    n = num * 3 + 1
    while True:
        if n % 2 == 0:
            n = n / 2
        else:
            n = n * 3 + 1
        if n == 1:
            break
        elif n == num:
            loopnums.append(num)
            break

print(loopnums)
print(len(loopnums))
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?