Mathematical puzzle to train the programmer's brain Q06 (modified version) Collatz's prediction

Problem summary

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

Mathematical puzzle to train the programmer's brain Q06 (modified version) Collatz's prediction
Mathematical puzzle to train the programmer's brain Q01 "Palindrome in decimal"
Mathematical puzzle to train the programmer's brain Q03 Turn the card over
Mathematical puzzles that train the programmer's brain Q08 Excellent cleaning robot
Mathematical puzzles that train the programmer's brain Q05 Still paying cash?
Mathematical puzzle to train the programmer's brain Q01 "Palindrome in decimal"
Mathematical puzzle to train the programmer's brain Q03 Turn the card over
Mathematical puzzle to train the programmer's brain Q06 (modified version) Collatz's prediction
Mathematical puzzles that train the programmer's brain Q08 Excellent cleaning robot
Mathematical puzzles that train the programmer's brain Q05 Still paying cash?