SharedExploration 1 / 2018-02-11-173918.sagewsOpen in CoCalc
%latex 
\noindent Exploration 1: The $3n+1$ Problem: If $n$ is odd, replace it by $3n+1$. If $n$ is even, divide it by 2. Repeat this cycle until an element is repeated

\bigskip 

\noindent (1) Find the length and terminating value for when \\
\indent $n =21$ \\
\indent $n =13$ \\
\indent $n =31$ \\
# defining the 3n+1 alogrithm to generate a list and determine its length
def threeN(n):
    list = []
    while n not in list:
        if Mod(n,2) == 0: #test for even
            list.append(n)
            n = n/2
        else:            # if not even it is odd
            list.append(n)
            n = 3*n+1
    length = len(list)
    return (length, list)

# threeN(21);
# threeN(13);
# threeN(31);
# print
# print
%latex




\noindent The length and terminating value of the $3n+1$ algorithm for: \\
\indent n = 21 is 8 and 1 \\
\indent n = 13 is 10 and 1 \\
\indent n = 31 is 107 and 1
n length termination value 9999 92 1
%latex

\noindent (2) Based upon running multiple tests for increasing values of $n$, it appears as though this algorithm terminates at a value of 1 for all natural numbers $n$.
# L(n) function.
def L(n):
    list = []
    while n not in list:
        if Mod(n,2) == 0: # test for even
            list.append(n)
            n = n/2
        else:            # if not even it is odd
            list.append(n)
            n = 3*n+1
    length = len(list)
    return length

# T(n) function.
def T(n):
    list = []
    while n not in list:
        if Mod(n,2) == 0: #test for even
            list.append(n)
            n = n/2
        else:            # if not even it is odd
            list.append(n)
            n = 3*n+1
    return list[-1]

print "n  ","L(n)","  T(n)"
for i in range(1, 101):
    print i, "  ",L(i),"   ", T(i)
n L(n) T(n) 1 3 2 2 3 4 3 8 1 4 3 1 5 6 1 6 9 1 7 17 1 8 4 1 9 20 1 10 7 1 11 15 1 12 10 1 13 10 1 14 18 1 15 18 1 16 5 1 17 13 1 18 21 1 19 21 1 20 8 1 21 8 1 22 16 1 23 16 1 24 11 1 25 24 1 26 11 1 27 112 1 28 19 1 29 19 1 30 19 1 31 107 1 32 6 1 33 27 1 34 14 1 35 14 1 36 22 1 37 22 1 38 22 1 39 35 1 40 9 1 41 110 1 42 9 1 43 30 1 44 17 1 45 17 1 46 17 1 47 105 1 48 12 1 49 25 1 50 25 1 51 25 1 52 12 1 53 12 1 54 113 1 55 113 1 56 20 1 57 33 1 58 20 1 59 33 1 60 20 1 61 20 1 62 108 1 63 108 1 64 7 1 65 28 1 66 28 1 67 28 1 68 15 1 69 15 1 70 15 1 71 103 1 72 23 1 73 116 1 74 23 1 75 15 1 76 23 1 77 23 1 78 36 1 79 36 1 80 10 1 81 23 1 82 111 1 83 111 1 84 10 1 85 10 1 86 31 1 87 31 1 88 18 1 89 31 1 90 18 1 91 93 1 92 18 1 93 18 1 94 106 1 95 106 1 96 13 1 97 119 1 98 26 1 99 26 1 100 26 1