Skip to main content
3 of 5
added 478 characters in body
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

(This isn't entirely valid because C++ doesn't allow nested functions. See the code below.)

Having fun with Hofstadter's Q-sequence! If we're using the radial distance from some point as the input and the output as the inverse colour, we get something which looks like coloured vinyl.

enter image description here

The sequence is very similar to the Fibonacci sequence, but instead of going 1 and 2 steps back in the sequence, you take the two previous values to determine how far to go back before taking the sum. It grows roughly linear, but every now and then there's a burst of chaos (at increasing intervals) which then settles down to an almost linear sequence again before the next burst:

enter image description here

You can see these ripples in the image after regions which look very "flat" in colour.

Of course, using only one colour is boring.

enter image description here

Now for the (not quite valid) code. I need the recursive function to compute the sequence, which I can't put inside the colour functions, unfortunately. However, if I did add the length of the preliminary code to each of the function bodies, they would be 139 bytes each:

int h[1000];
int f(int n){if(!h[n])h[n]=n<2?1:f(n-f(n-1))+f(n-f(n-2));return h[n];}
char red_fn(int i,int j){
    return 256-f(sqrt((i-256)*(i-256)+(j-256)*(j-256))/2.9);
}
char green_fn(int i,int j){
    return 256-f(sqrt((i-512)*(i-512)+(j-768)*(j-768))/2.9);
}
char blue_fn(int i,int j){
    return 256-f(sqrt((i-768)*(i-768)+(j-256)*(j-256))/2.9);
}

Of course, this is pretty much the simplest possible usage of the sequence. Feel free to borrow it and do other crazy things with it!

Here is another version where the boundary and the colours are determined by the Q-sequence.

char red_fn(int i,int j){
    return 2*f(i)-i+512>1023-j?f(i)/2.4:f(1023-i)/2.4;
}
char green_fn(int i,int j){
    return 2*f(i)-i+512>1023-j?f(i)/2.4:f(1023-i)/2.4;
}
char blue_fn(int i,int j){
    return 2*f(i)-i+512>1023-j?f(i)/2.4:f(1023-i)/2.4;
}

enter image description here

Martin Ender
  • 197.2k
  • 67
  • 447
  • 975