# Project 1 Calculations

## 301 days ago by sehunt2000

The Collatz conjecture, as known as the Hailstone sequence, is the 3n+1 problem which can be defined as the following function f(n) where n must be a positive integer greater than one and f(n) is used to produce the next value in the Collatz conjecture.

def f(n): #defines f(n) if n.is_integer() and n >0: #checks if n is a positive integer greater than 1 if n==1: return(0) elif is_odd(n): return(3*n+1) else: return(n/2) else: return("Please enter a positive integer.")
f(6)
 3 3
f(-2)
 'Please enter a positive integer.' 'Please enter a positive integer.'
f(9)
 28 28

To obain all iterations within a specific n value's Collatz conjecture, the equation f(n) is used continuously until terminated by the values 4-2-1. Sage is a very useful tool when computing conditonal values as such, especially larger values with lots of iterations.

def f_seq(n): # function to display sequence of Hailstone numbers for n L = [n] # sets values in brackets x = n while x != 1: # checks if input is not 1 x=f(x) # plugs input into f equation L.append(x) # adds vaule to existing list return (L)
f_seq(12)
 [12, 6, 3, 10, 5, 16, 8, 4, 2, 1] [12, 6, 3, 10, 5, 16, 8, 4, 2, 1]
f_seq(29)
 [29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] [29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
f_seq(44)
 [44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] [44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]

What if we only want the value for the number of iterations it takes f(n) to reach the Collatz conjecture?

def f_its(n): # defining function to calculate number of iterations x = n its = 1 # creates a starting point for counting at 1 to consider original n while x != 1: x=f(x) its = its + 1 return(its)
f_its(12)
 10 10
f_its(50)
 25 25
f_its(429)
 103 103
f_its(29)
 19 19

Now that we can calculate the value of f(n), the entire range of values for f(n), and the number of iterations of f(n), we will observe their behavior linearly.

hailstone=[[1,0]] # defines initial point champions=[] # creates variable for outliers max_value_seen=3 # creates max value variable for i in srange(2,10001): # applies range for vaules stepcounter=0 n=i while n> 1: # while loop to generate x component n=f(n) # sets n into f(n) stepcounter+=1 # counter to obtain number of iterations if stepcounter> max_value_seen: # if statement to check for outliers max_value_seen = stepcounter # sets max value to counter when it occurs champions.append([i,max_value_seen]) # correlates max value to champions hailstone.append([i,stepcounter])

Here we can see the hailstone values for n with a range up to 10,000. Although f(12) only has 10 iterations to reach the Collatz conjecture, we can observe all iterations of every value produced.

list_plot(hailstone[12:10000]) list_plot(hailstone[12:500]) list_plot(hailstone[12:100]) Notice, there are values far from the general trend of log and decay curves. These values are called champions, which take longer than any other iterations within f(n).

list_plot(champions) We can see that the champion values have a general pattern of the log curve. Why is this important? Taking this for example and looking back at the previous hailstone plots, we can see that 2 general patterns occur, a positve logrithmic curve a negative decay curve. What does tell us about the iterations of f(n)?