# Collatz Conjecture

## Main Question or Discussion Point

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.

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?

-Joe

Related Linear and Abstract Algebra News on Phys.org
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:
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.

Thanks for the suggestions! I didn't even think of GNUPlot!

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:
Can you post your code for that? I'd greatly appreciate it.

For generating the values? Or the graph?

Here is one in plain C, and another in C#. Both will produce a data file, with which gnuplot reproduces the graphs above.

Code:
#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:
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);
}

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);
}
}
}

For generating the values? Or the graph?
The graph. :)

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.