• Home
  • Readings
  • Github
  • MIES
  • TmVal
  • About
Gene Dan's Blog

No. 115: The Collatz Conjecture

22 November, 2016 7:52 PM / Leave a Comment / Gene Dan

The Collatz Conjecture is a famous unsolved problem in mathematics. Given any positive integer, if that integer is even, divide it by two. If it’s odd, multiply it by three and then add 1. Keep repeating until you get 1. The conjecture claims that no matter what number you start with, you will always reach 1.

Is this the case? Nobody knows! But let’s try a few examples: 12, 7, and 9

12 -> 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
7 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
9 -> 28 -> 14 -> 7 -> … 8 -> 4 -> 2 -> 1

In all three cases, we get a chain of numbers that eventually leads to 1. In fact, we can try every integer up to 100 million and we will still end up with 1! Given that a counterexample hasn’t been found for extremely large numbers, many people believe the conjecture to be true. The reason why this problem is so famous is because it’s easy to understand, but hard to solve.

Given the example above, we can construct a directed graph that plots each successive step for all situations up to a specified integer. For example, when n = 50:

selection_088

We can see that every chain leads to 1, which is why so many arrows point to node 1. I’ve created a visualization using R Shiny, and using the slider below, you can see how the graph changes as you change n. I could go on about R Shiny, which is an extremely useful package for visualizing mathematical concepts due to its interactive nature, but since I’m busy, I’ll have to save that for a later time.

The code is contained in 3 files, server.R, ui.r, and helpers.R.

server.R:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library(shiny)
library(igraph)
source("helpers.R")
shinyServer(function(input, output) {
  
  output$distPlot <- renderPlot({
    out.n <- input$n
    pairs <- c()
    for(i in 1:out.n)
    {
      curr <- i
      while(curr != 1)
      {
        if(curr %% 2 == 0)
        {
          nxt <- curr / 2
        }
        else
        {
          nxt <- 3 * curr + 1
        }
        pairs <- c(pairs,as.character(curr),as.character(nxt))
        curr <- nxt
      }
    }
    graph.directed <- graph(pairs)
    l <- layout.forceatlas2(graph.directed, iterations=100,plotstep=0)
    plot(graph.directed,layout=l, vertex.color="skyblue", vertex.size=6, edge.arrow.size=.2, vertex.label.cex=.5)
  })
  
})

ui.r

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
library(shiny)
 
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Collatz Conjecture"),
  withMathJax(
  HTML("Consider the following operation on an arbitrary positive integer:
            <ul>
            <li>If the number is even, divide it by two.</li>
            <li>If the number is odd, triple it and add one.</li>
            </ul></br>
  In modular arithmetic notation, define the function \\(f\\) as follows:</br>
$$f(x)= \\left\\{
\\begin{aligned}
     \\ n/2  &\\quad  \\text{if } n\\equiv0\\,(\\text{mod } 2)  \\\\
     \\ 3n+1 &\\quad  \\text{if } n\\equiv1\\,(\\text{mod } 2)  \\\\
     \\end{aligned}
     \\right.$$
  Now, form a sequence by performing this operation repeatedly, beginning with any positive integer, and taking the result at each step as the input at the next.
       In notation:
$$a_i=\\left\\{
       \\begin{aligned}
       \\ n          & \\quad \\text{for } i = 0 \\\\
       \\ f(a_{i-1}) & \\quad \\text{for i} >0 \\\\
       \\end{aligned}
       \\right.$$
  The Collatz conjecture is: <i>This process will eventually reach the number 1, regardless of which positive integer is chosen initially</i>.")),
  
  # Sidebar with a slider input for K
  sidebarLayout(
    sidebarPanel(
       sliderInput("n",
                   "Directed graph for all sequences up to n:",
                   min = 2,
                   max = 100,
                   value = 50)
    ),
    
    
    mainPanel(
       plotOutput("distPlot",width="500px", height="500px")
    )
  )
))

The helpers.R file was taken here, and was used to apply the ForceAtlas 2 layout on the igraph package.

Posted in: Mathematics

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Post Navigation

← Previous Post
Next Post →

Archives

  • September 2023
  • February 2023
  • January 2023
  • October 2022
  • March 2022
  • February 2022
  • December 2021
  • July 2020
  • June 2020
  • May 2020
  • May 2019
  • April 2019
  • November 2018
  • September 2018
  • August 2018
  • December 2017
  • July 2017
  • March 2017
  • November 2016
  • December 2014
  • November 2014
  • October 2014
  • August 2014
  • July 2014
  • June 2014
  • February 2014
  • December 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • March 2013
  • January 2013
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • January 2011
  • December 2010
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • September 2009
  • August 2009
  • May 2009
  • December 2008

Categories

  • Actuarial
  • Cycling
  • Logs
  • Mathematics
  • MIES
  • Music
  • Uncategorized

Links

Cyclingnews
Jason Lee
Knitted Together
Megan Turley
Shama Cycles
Shama Cycles Blog
South Central Collegiate Cycling Conference
Texas Bicycle Racing Association
Texbiker.net
Tiffany Chan
USA Cycling
VeloNews

Texas Cycling

Cameron Lindsay
Jacob Dodson
Ken Day
Texas Cycling
Texas Cycling Blog
Whitney Schultz
© Copyright 2025 - Gene Dan's Blog
Infinity Theme by DesignCoral / WordPress