Following Poromenos’ nifty “Hello World!” function, Jan Krueger posted a great explanation for why it works along with a general method for producing a trigonometric function for _any_ string you like:

The magic behind Poromenos’s function is the Fourier theorem: any “mostly” continuous and periodic function can be expressed as a sum of sines and cosines. I’m not going to bore you with the details; suffice to say that this also works for sampled functions, i.e. discrete series of values.

There’s an algorithm called DFT (Discrete Fourier Transform) that takes a series of N complex sample values and generates a corresponding Fourier series which encodes the various sine/cosine coefficients in N complex output values. In the special case of real input values (which is an extremely common case), you can effectively throw away half of these output values and take the remaining N real/imaginary components, do a bit of magic, and end up with coefficients for a function of the form:

f(t) = x0 + x1 cos(t) + x2 sin(t) + x3 cos(2t) + x4 sin(2t) + …

Now, f(2 pi n/N) returns exactly the (n+1)th character of the original string.

Follow the link and you’ll find a nifty C program that will produce a trigonometric function for any string you like.

HOWTO: encode a string into a complicated-looking trigonometric function

  • I completely dumbfounded one of my coworkers with this today.

    I completely dumbfounded one of my coworkers with this today. He tried to work it out in his head for about fifteen minutes before finally giving up and running it.

  • Poromenos

    I never realized it could be used to dumbfound your colleagues, now I will have to start encoding random strings this way :P

    Also, T-shirts of the function are finally available at in various shapes!