Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Collatz Conjecture

  1. Oct 24, 2011 #1
    Hello there everyone!

    I've written a lovely little program to go through the tedious process of testing numbers in the "If odd 3n+1; If even n/2; Repeat." scenario. It then saves all of the numbers, starting with the integer being tested, and ending with "1" in a file of the form "number_that_you_tested[How_Many_Steps_It_Took]. IE: 3[7].

    I understand that much more rigorous testing has been done on this problem, and that there is little to no chance of discovering anything new. I'm still inexplicably enthusiastic about the problem. Anyway, my question is: What would be the best method of plotting the number of steps that each integer takes? Meaning, what technical solution would be best for graphing this? Some sort of javascript sorcery perhaps?

    Thanks in advance

    -Joe
     
  2. jcsd
  3. Oct 26, 2011 #2
    Have your program output data as a two-column list, then import the data into gnuplot/Mathematica/whatever you want and plot column 1 vs. column 2.

    Let's say you run your program on the integers from 1 to 6. Then your data file looks like:

    1 0
    2 1
    3 7
    4 2
    5 5
    6 8

    and in the plotting program, you'd do something like this:

    [PLAIN]http://img641.imageshack.us/img641/6260/unledvvk.png [Broken]

    In gnuplot, you'd say something like 'plot "data.dat" using 1:2' and get the same result.

    Whatever program you decide to use, it'll be the same general idea.

    Hope this helps.
     
    Last edited by a moderator: May 5, 2017
  4. Oct 26, 2011 #3
    Matlab or its open source alternative (free), Octave, is good for stuff like this. I actually made an Octave script for this once, but I lost it when my HDD crashed.

    Mathematica is also good, but there isn't an open source/free alternative that is even close to equivalent.
     
  5. Oct 26, 2011 #4
    Thanks for the suggestions! I didn't even think of GNUPlot!
     
  6. Oct 29, 2011 #5
    This is great! I've made my graph a bit smaller just for the sake of making it a more reasonable size for demonstration here, I've managed to create graphs of 1 million+ values, and it works great. I'm wondering, however, what the best way to go about creating a function that would represent these values is.

    I see a pattern, it looks like it may be a square root or a logarithm. Any help would be appreciated. This may not be the best version of the graph to see the trend, but you'll get the idea.

    www.matcatsmp.com/collatz.png[/URL]

    Edit:

    Here's ten thousand of the values on a smaller image to better illustrate the pattern...

    [PLAIN]www.matcatsmp.com/condensedcollatz.png[/URL]
     
    Last edited by a moderator: Apr 26, 2017
  7. Oct 29, 2011 #6
    Can you post your code for that? I'd greatly appreciate it.
     
  8. Oct 29, 2011 #7
    For generating the values? Or the graph?
     
  9. Oct 29, 2011 #8
    Here is one in plain C, and another in C#. Both will produce a data file, with which gnuplot reproduces the graphs above.

    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>

    void    TestCollatz (int n);

    int main (int argc, char* argv[])
    {
        int maxN;
        int n;

        if (argc != 2) {
            fprintf (stderr, "Usage: collatz maxN > data.txt\n");
            return 1;
        }
       
        maxN = atoi (*++argv);
       
        for (n = 1; n <= maxN; n++)
            TestCollatz (n);

        return 0;
    }

    void    TestCollatz (int n)
    {
        int count = 0;

        printf ("%d\t", n);

        while (n != 1) {
            count++;

            if (n & 0x1)
                n = 3 * n + 1;
            else
                n >>= 1;
        }

        printf ("%d\n", count);
    }
    Code (Text):
    using System;

    namespace Collatz
    {
        class App
        {
            public static void Main (string[] args)
            {
                if (args.Length != 1)
                {
                    Console.WriteLine("Usage: Collatz maxN > data.txt");
                    return;
                }

                new App(args[0]);
            }
           
            public App(string arg)
            {
                try
                {
                    int maxN = int.Parse(arg);
                   
                    for (int n = 1; n <= maxN; n++)
                        TestCollatz (n);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
           
            private void TestCollatz(int n)
            {
                int count = 0;
               
                Console.Write(n + "\t");
               
                while (n != 1)
                {
                    count++;
                   
                    if ((n & 0x1) == 0)
                        n >>= 1;
                    else
                        n = 3 * n + 1;
                }
               
                Console.WriteLine(count);
            }
        }
    }
     
  10. Oct 29, 2011 #9
    The graph. :)
     
  11. Oct 29, 2011 #10
    I used gnuplot to parse a text file that I generated with a halfway hacked mpi thingy that runs perl scrips in parallel to generate them. Then, just plain old gnuplot.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook