Skip to content

Instantly share code, notes, and snippets.

@tsulej
Created May 19, 2016 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsulej/9c4bf3175803e9b440035f37f4551cf2 to your computer and use it in GitHub Desktop.
Save tsulej/9c4bf3175803e9b440035f37f4551cf2 to your computer and use it in GitHub Desktop.
Collatz conjecture vizualization
// collatz conjecture visualization
// generateme.tumblr.com
void setup() {
size(2048, 500);
background(20);
stroke(220, 100);
strokeWeight(0.6);
smooth(8);
}
float ang = 0; // inital angle
float step = TWO_PI/200.0; // angle step
int c0 = 100000; // initial number for sequence
void draw() {
for (int i=0; i<200; i++) {
int c = c0;
int x = (c0-100000);
int y = 400;
// if c==4, sequence is cyclic
// hypothesis says, every collatz sequence ends with cycle 4,2,1,4,...
while (c!=4) {
if ((c&1)>0) {
ang+=step;
c=3*c+1;
} else {
ang-=step;
c >>= 1;
}
x+=cos(ang);
y+=sin(ang);
point(x/5.0, y);
}
c0++; // next sequence
}
}
void keyPressed() {
saveFrame("res2#####.png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment