Collatz Conjecture: Plotting Integer Steps with Joe

Click For Summary

Discussion Overview

The discussion revolves around the Collatz Conjecture, specifically focusing on methods for plotting the number of steps each integer takes to reach 1 using the conjecture's iterative process. Participants explore various technical solutions for graphing the results generated by a program designed to test integers according to the conjecture's rules.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • Joe shares a program he created to test integers based on the Collatz Conjecture and seeks advice on the best method for plotting the resulting data.
  • One participant suggests outputting data as a two-column list for use in plotting software like gnuplot or Mathematica.
  • Another participant recommends using Matlab or its open-source alternative, Octave, for plotting, mentioning a previous experience with Octave.
  • Joe expresses gratitude for the suggestions and mentions successfully creating graphs for large datasets, while inquiring about a function to represent the plotted values.
  • Joe observes a potential pattern in the data that may resemble a square root or logarithmic function and seeks further assistance.
  • One participant requests the code used for generating the values or the graph.
  • Another participant provides code examples in C and C# that generate the data file for plotting.
  • A participant mentions using gnuplot with a parallel processing approach to generate data efficiently.

Areas of Agreement / Disagreement

Participants generally agree on the usefulness of various plotting tools and programming languages for visualizing the Collatz Conjecture data. However, there is no consensus on the best method for creating a function to represent the plotted values, as opinions on the nature of the pattern observed remain open-ended.

Contextual Notes

Some participants note the potential complexity of finding a mathematical function to represent the plotted values, indicating that further exploration is needed to confirm any patterns.

clanijos
Messages
27
Reaction score
0
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
 
Physics 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

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. :)
 
  • #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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K