Disclaimer
All programs below are free software: you can redistribute them and/or
modify them under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

These program are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
Boids (boids.c)

An OpenGL/GLUT based application featuring 32 grey cones that fly around in self-organizing flocks chasing 3 colored balls that bounce off the walls of a wire frame cube.





Craig Reynolds (their inventor) has an informative page here.


Mandelbrot set PNG image generator (msp.c)

Uses libpng to create images of the Mandelbrot set. The image at right, which depicts the region { z = x + i y : -1.0625 ≤ x ≤ -0.9375, 0.2425 ≤ y ≤ 0.3675 }, was generated by the command
$ msp 640 640 -1.0625 -0.9375 0.2425 0.3675 > msp.png


freq - word frequency counter (freq.c, ctbl.c, ctbl.h)
$ echo "foo bar buz qux buz bar bar qux qux bar qux" | freq
bar 4
qux 4
foo 1
buz 2
$ freq < hhgttg.txt | sort --key=2 --numeric-sort --reverse | head --lines=20
the 2224
of 1253
to 1175
a 1121
and 1107
said 680
it 665
was 605
in 590
he 546
that 520
you 495
I 428
on 350
Arthur 332
his 324
Ford 314
at 305
The 304
for 283
$ freq < hhgttg.txt | grep Zaphod
Zaphod's 8
Zaphod 206

Representations by sums of squares (rkn.c)

Let rk(n) denote the number of representations of n as a sum of k squares,
rk(n) = #{(x1, x2, ..., xk) ∈ ℤk : x12 + x22 + ... + xk2 = n}
The simplest possible implementation (for fixed k) is the order k⋅(2n)k function
int rk ( int n )
{
  int r = 0, x1, x2, ..., xk;
  for (x1 = -n; x1 <= n; x1++)
    for (x2 = -n; x2 <= n; x2++)
      ...
        for (xk = -n; xk <= n; xk++)
          if (x1*x1 + x2*x2 + ... + xk*xk == n)
            r++;
  return r;
}
To improve upon this, we exploit the relationship
rk(n) = rk-1(n) + 2 ⋅ ∑j rk-1(n-sj)
where sj runs through the nonzero squares upto n.

Counting representations by sums of squares is an old problem; the Mathworld article Sum of Squares Function provides a fairly good overview of its history.

John Hammond