Find the number of even numbers less than 10000 that return to themselves by repeating the following operations.
--For the first time, multiply by 3 and add 1 ――From the second time onwards --For even numbers, divide n by 2. --For odd numbers, multiply n by 3 and add 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))
Recommended Posts