2022.03.18 MATH 3600 3n+1

106 days ago by calkin

Investigating the 3n+1 conjecture.

Define f(n)= n/2 if n is even, 3n+1 if n is odd.

Iteratively apply f starting at a value k.  Do we always end up at 4,2,1?

def f(n): if is_even(n): return(n/2) else: return(3*n+1) 
       
n=17 while n>1: n=f(n) print(n) 
       
52
26
13
40
20
10
5
16
8
4
2
1
52
26
13
40
20
10
5
16
8
4
2
1
is_even(n) 
       
False
False
n=17 counter = 0 while n>1: n=f(n) counter+=1 print(counter) 
       
12
12
for m in srange(2,30): n=m counter=0 while n>1: n=f(n) counter+=1 print(m, counter) 
       
(2, 1)
(3, 7)
(4, 2)
(5, 5)
(6, 8)
(7, 16)
(8, 3)
(9, 19)
(10, 6)
(11, 14)
(12, 9)
(13, 9)
(14, 17)
(15, 17)
(16, 4)
(17, 12)
(18, 20)
(19, 20)
(20, 7)
(21, 7)
(22, 15)
(23, 15)
(24, 10)
(25, 23)
(26, 10)
(27, 111)
(28, 18)
(29, 18)
(2, 1)
(3, 7)
(4, 2)
(5, 5)
(6, 8)
(7, 16)
(8, 3)
(9, 19)
(10, 6)
(11, 14)
(12, 9)
(13, 9)
(14, 17)
(15, 17)
(16, 4)
(17, 12)
(18, 20)
(19, 20)
(20, 7)
(21, 7)
(22, 15)
(23, 15)
(24, 10)
(25, 23)
(26, 10)
(27, 111)
(28, 18)
(29, 18)

Exercise: construct a function hailstone(n) which computes how many iterations of f to reach 1.

Plot hailstone.  What do you see?  What do you notice?  What questions arise?

def hailstone(n): counter = 0 while n>1: n=f(n) counter+=1 return(counter) 
       
hailstone(27) 
       
111
111
hailstone_list=[] for i in srange(2,10000): hailstone_list.append([i,hailstone(i)]) 
       
list_plot(hailstone_list) 
       
champions=[] max=0 for i in srange(2,100000): h=hailstone(i) if h>max: champions.append([i,h]) max=h 
       
print(champions) 
       
[[2, 1], [3, 7], [6, 8], [7, 16], [9, 19], [18, 20], [25, 23], [27,
111], [54, 112], [73, 115], [97, 118], [129, 121], [171, 124], [231,
127], [313, 130], [327, 143], [649, 144], [703, 170], [871, 178], [1161,
181], [2223, 182], [2463, 208], [2919, 216], [3711, 237], [6171, 261],
[10971, 267], [13255, 275], [17647, 278], [23529, 281], [26623, 307],
[34239, 310], [35655, 323], [52527, 339], [77031, 350]]
[[2, 1], [3, 7], [6, 8], [7, 16], [9, 19], [18, 20], [25, 23], [27, 111], [54, 112], [73, 115], [97, 118], [129, 121], [171, 124], [231, 127], [313, 130], [327, 143], [649, 144], [703, 170], [871, 178], [1161, 181], [2223, 182], [2463, 208], [2919, 216], [3711, 237], [6171, 261], [10971, 267], [13255, 275], [17647, 278], [23529, 281], [26623, 307], [34239, 310], [35655, 323], [52527, 339], [77031, 350]]
list_plot(champions) 
       
for c in champions: print(c[0],factor(c[0]),c[1]) 
       
(2, 2, 1)
(3, 3, 7)
(6, 2 * 3, 8)
(7, 7, 16)
(9, 3^2, 19)
(18, 2 * 3^2, 20)
(25, 5^2, 23)
(27, 3^3, 111)
(54, 2 * 3^3, 112)
(73, 73, 115)
(97, 97, 118)
(129, 3 * 43, 121)
(171, 3^2 * 19, 124)
(231, 3 * 7 * 11, 127)
(313, 313, 130)
(327, 3 * 109, 143)
(649, 11 * 59, 144)
(703, 19 * 37, 170)
(871, 13 * 67, 178)
(1161, 3^3 * 43, 181)
(2223, 3^2 * 13 * 19, 182)
(2463, 3 * 821, 208)
(2919, 3 * 7 * 139, 216)
(3711, 3 * 1237, 237)
(6171, 3 * 11^2 * 17, 261)
(2, 2, 1)
(3, 3, 7)
(6, 2 * 3, 8)
(7, 7, 16)
(9, 3^2, 19)
(18, 2 * 3^2, 20)
(25, 5^2, 23)
(27, 3^3, 111)
(54, 2 * 3^3, 112)
(73, 73, 115)
(97, 97, 118)
(129, 3 * 43, 121)
(171, 3^2 * 19, 124)
(231, 3 * 7 * 11, 127)
(313, 313, 130)
(327, 3 * 109, 143)
(649, 11 * 59, 144)
(703, 19 * 37, 170)
(871, 13 * 67, 178)
(1161, 3^3 * 43, 181)
(2223, 3^2 * 13 * 19, 182)
(2463, 3 * 821, 208)
(2919, 3 * 7 * 139, 216)
(3711, 3 * 1237, 237)
(6171, 3 * 11^2 * 17, 261)
N=1000000 hailstone_dict={1:0} for i in srange(2,N): n=i counter=0 while hailstone_dict.has_key(n)==False: counter+=1 n=f(n) hailstone_dict[i]=counter+hailstone_dict[n] 
       
list_plot(hailstone_dict) 
       
hailstone_dict[27] 
       
111
111
champions=[] max=0 for i in srange(2,N): h=hailstone_dict[i] if h>max: champions.append([i,h]) max=h 
       
list_plot(champions) 
       
n=27 while n>1: n=f(n) print(n) 
       
82
41
124
62
31
94
47
142
71
214
107
322
161
484
242
121
364
182
91
274
137
412
206
103
310
155
466
233
700
350
175
526
263
790
395
1186
593
1780
890
445
1336
668
334
167
502
251
754
377
1132
566
283
850
425
1276
638
319
958
479
1438
719
2158
1079
3238
1619
4858
2429
7288
3644
1822
911
2734
1367
4102
2051
6154
3077
9232
4616
2308
1154
577
1732
866
433
1300
650
325
976
488
244
122
61
184
92
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1
82
41
124
62
31
94
47
142
71
214
107
322
161
484
242
121
364
182
91
274
137
412
206
103
310
155
466
233
700
350
175
526
263
790
395
1186
593
1780
890
445
1336
668
334
167
502
251
754
377
1132
566
283
850
425
1276
638
319
958
479
1438
719
2158
1079
3238
1619
4858
2429
7288
3644
1822
911
2734
1367
4102
2051
6154
3077
9232
4616
2308
1154
577
1732
866
433
1300
650
325
976
488
244
122
61
184
92
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1