Printing "Hello world!" using curve fitting techniques (or: The "Hello world!" function)

Well, I have a computer architecture exam in six hours and can’t be bothered, so I figured I would realize a lifelong dream of mine, and make a program that prints “Hello world!” using curve fitting techniques. Enlisting the help of a good friend with numerous mathematical papers under his belt (ostensibly because he could not afford a tighter belt), MATLAB and a longing for procrastination, we embarked on this perilous journey. After many, many hours of fitting and discarding data, I can finally present to you my masterpiece.

This function returns the ascii ordinal for each of the letters of “Hello, world!” at each integer position (0, 1, 2, 3 etc). I call it… The “Hello world!” function! A simple Python script shows it in all its glory:

from math import *

def f(x):
    return int(round(96.75 + -21.98*cos(x*1.118) + 13.29*sin(x*1.118) + -8.387*cos(2*x*1.118)\
               + 17.94*sin(2*x*1.118) + 1.265*cos(3*x*1.118) + 16.58*sin(3*x*1.118)\
               + 3.988*cos(4*x*1.118) + 8.463*sin(4*x*1.118) + 0.3583*cos(5*x*1.118)\
               + 5.878*sin(5*x*1.118)))

print "".join([chr(f(x)) for x in range(12)])

and that’s it! Just save this as hello.py and run it, and the phrase “Hello world!” will be prominently etched upon your screen for future generations to enjoy. I, along with my friend and colleague, reserve all rights to this code, which will be coming soon to a scientific conference near you. I will also be waiting for my Nobel prize nomination.

In case you are wondering, here are the plot points and the fitted function:

Thank you all for your support. T-shirts of the function are available at http://www.cafepress.com/poromenos, right now there’s only the classic MATLAB design but I soon hope to be making better ones.

EDIT: My good friend Iain (of Lasertoast fame) has sent me this fantastic etching of the curve on stainless steel, so now I’ll try to milk him for all the etchings I can. Drop them a line if you want anything etched, I can guarantee your satisfaction.

Brilliant

Thanks for this. It made my day.

Let me know when you have the plot for sale in t-shirt format.

Submitted by Jason Striegel (not verified) on Thu, 03/04/2008 - 05:00.
am gonna buy t-shirt too

hehe, am gonna buy t-shirt too;)

Submitted by Jamsy (not verified) on Thu, 15/01/2009 - 19:03.
DSP Hello World

Kind of Fourier series applied to Hello World? Very nice. It can be a very useful example to show to someone that any periodic function can be a sum of sines and cosines. Make my d... morning (I hope I will find something for the afternoon). Thanks.

Submitted by Emmanuel Rio (not verified) on Thu, 03/04/2008 - 08:11.
Re: DSP

Indeed, that's exactly it. There were some series that were fluctuating wildly and hit the spots pretty much by accident, but this one fits beautifully with a 0.01 margin of error or something (which obviously goes away when rounding).
---
Vidi, Vici, Veni.

Submitted by Stavros on Thu, 03/04/2008 - 10:25.
The genius of it makes my

The genius of it makes my eyes bleed.
I'll be forwarding the bill for my optometrist fees shortly.

Well done sir.

Submitted by Mr L (not verified) on Thu, 03/04/2008 - 11:13.
Fees

Pay nothing!

Submitted by Moriarty (not verified) on Fri, 16/05/2008 - 21:38.
Better equation

Good idea...but I've seen it elsewhere before :|

A method that will give you a better-looking polynomial (equation-wise) can be obtained via Lagrange Interpolation:

http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html

Of course, the resulting graph may look nasty just like almost all other high order polynomials (probably will look like a bunch of vertical lines) but the equation itself will consist solely of integers! :)

Submitted by wolfier (not verified) on Thu, 03/04/2008 - 20:04.
Re: Equation

I tried that, it did look like a bunch of vertical lines (which might introduce unacceptable floating-point errors, since it varies so wildly), but I am rather partial to this one... It's so pretty!
---
Vidi, Vici, Veni.

Submitted by Stavros on Fri, 04/04/2008 - 10:53.
Spam-analysis

Have you already tried to use this algorythm for spam-visualisation?
Could be smth like hier: http://www.wired.com/special_multimedia/2008/ff_dataart_1603

Submitted by sohin@drupal.org on Wed, 09/04/2008 - 17:31.
Ruddy Awesome

Amazing. I'm wondering how many hours (out of the six you had) it took you.

Submitted by Scatman Dave (not verified) on Fri, 11/04/2008 - 20:29.
Re: Awesome

About twenty minutes or so (but don't tell anyone)!
---
Vidi, Vici, Veni.

Submitted by Stavros on Fri, 11/04/2008 - 21:07.
Haskell version

I saw a common pattern in the function, so I abstracted it into another function "g".


import Char
f x = let n = g (-21.98) 13.29 1 + g (-8.387) 17.94 2 +
              g 1.265 16.58 3 + g 3.988 8.463 4 +
              g 0.3583 5.878 5 + 96.75
          g a b c = a * cos (c * x * 1.118) +
                    b * sin (c * x * 1.118) in
      round n
main = putStrLn $ map chr $ map f [0..11]

Submitted by kinghajj (not verified) on Wed, 29/10/2008 - 03:49.
Haskell variation

Here's a variation:


{-# LANGUAGE ParallelListComp #-}
module Main where
import Data.Char

hello x =
let x' = x * 1.118 in round $
96.75 + sum
[ a*cos(n*x') + b*sin(n*x')
| n <- [1..5]
| a <- [-21.98, -8.387, 1.265, 3.988, 0.3583]
| b <- [13.29, 17.94, 16.58, 8.463, 5.878]
]

main = putStrLn $ map (chr.hello) [0..11]

Submitted by RayNbow (not verified) on Fri, 22/05/2009 - 13:58.
Function Liberation Front

JavaScript (1.7+) version.

Submitted by chocolateboy (not verified) on Wed, 29/10/2008 - 18:04.
this is so fucking stupid I

this is so fucking stupid I wish I was dead

Submitted by Anonymous (not verified) on Wed, 29/10/2008 - 18:34.
Luckily, that can easily be

Luckily, that can easily be arranged.

Submitted by Stavros on Thu, 30/10/2008 - 11:54.
Note: the proper possessive

Note: the proper possessive of "Poromenos" is "Poromenos's", unless you are a classical character. You are not a classical character.

Submitted by enauv (not verified) on Fri, 22/05/2009 - 01:13.
Re: Possessive

How do you know!?! Also, I've read that either works, do you have a URL?

Submitted by Stavros on Fri, 22/05/2009 - 11:01.
Nice T-shirt..I just wanna to

Nice T-shirt..I just wanna to got it.

Thnaks
watch tv episodes online

Submitted by watch movies online (not verified) on Sun, 31/05/2009 - 04:20.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Ads