Friday, March 5, 2010

Another reason I hope girls don't read this

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* The Collatz Conjecture states that if you pick a number,
* and if its even divide it by two and if its odd multiply
* it by three and add one, and you repeat this procedure
* long enough, eventually your friends will stop calling
* to see if you want to hang out.
*
* http://xkcd.com/710/
*
*/
public class Collatz {

/**
* rely on finite stack as failsafe to prevent running forever.
* (this system appears to always terminate anyway, though).
*
* @param x
*/
public static void collatz(long x){

System.out.println(x);

if(x == 1){
/* the next number is actually 4
* (depending on your religion)
* but then it would run forever
*/
return;
}else if(x%2 == 0){
collatz( x/2);
}else
{
collatz( x*3+1);
}
}

public static void main(String[] args) throws Exception{

System.out.println("Enter an integer: " );

collatz(
Long.parseLong( new BufferedReader(new InputStreamReader(System.in)).readLine() )
);
}
}



I theorize that for any integer, this function will always reach 1, and I have no intentions of proving that. Also I made a graph, and also I'd like to graph the depth of this function, where the x axis is an integer, and the y axis is the count of iterations before you reach 1.


This post will be continued after lunch, and after I do some actual work....

// http://www.youtube.com/watch?v=FJRMwdmFYWg&feature=popular


[Edit]
You know you want to: http://igraph.sourceforge.net/screenshots.html

No comments:

Post a Comment