# Collatz Conjecture: Plotting Integer Steps with Joe

• clanijos
In summary: I've since managed to do it a little more easily, but I don't have the code on this computer. I'll be able to get it tomorrow if you still need it, though.
clanijos
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?

-Joe

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

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

clanijos said:
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.

## 1. What is the Collatz Conjecture?

The Collatz Conjecture is a mathematical problem that was first proposed by German mathematician Lothar Collatz in 1937. It involves taking any positive integer and repeatedly applying a formula to it until it reaches 1.

## 2. What is the formula used in the Collatz Conjecture?

The formula used in the Collatz Conjecture is as follows: if the number is even, divide it by 2, and if the number is odd, multiply it by 3 and add 1. This process is then repeated with the resulting number until it reaches 1.

## 3. Has the Collatz Conjecture been proven?

No, the Collatz Conjecture has not been proven. It has been tested for a large number of integers, but no one has been able to prove that it holds true for all numbers.

## 4. Why is the Collatz Conjecture significant?

The Collatz Conjecture is significant because it is a seemingly simple problem that has yet to be solved. It has been studied by mathematicians for decades and has sparked new approaches and techniques in the field of mathematics.

## 5. What is the purpose of "Plotting Integer Steps with Joe" in relation to the Collatz Conjecture?

"Plotting Integer Steps with Joe" is a way to visually represent the steps involved in the Collatz Conjecture. It allows for a better understanding of the problem and can potentially lead to new insights and patterns in the data.

• General Math
Replies
7
Views
2K
• Linear and Abstract Algebra
Replies
1
Views
3K
• Linear and Abstract Algebra
Replies
2
Views
4K
• Linear and Abstract Algebra
Replies
2
Views
4K
• Linear and Abstract Algebra
Replies
6
Views
7K
• General Math
Replies
1
Views
2K
• Linear and Abstract Algebra
Replies
2
Views
3K
• Linear and Abstract Algebra
Replies
5
Views
4K
• Programming and Computer Science
Replies
9
Views
1K
• Set Theory, Logic, Probability, Statistics
Replies
1
Views
3K