Skip to main content


Showing posts from February, 2008

The sum of the first n odd numbers is always a square

I was staring at the checked pattern on the back of an airline seat the other day when I suddenly saw that the sum of the first n odd numbers is always a square. For example, 1 1 + 3 = 4 1 + 3 + 5 = 9 1 + 3 + 5 + 7 = 16 And, of course, it occurred to me that it would be nice to be able to prove it. There are lots of ways to do that. Firstly, this is just the sum of an arithmetic progression starting at a = 1 with a difference of d = 2 . So the standard formula gives us: sum_odd(n) = n(2a + (n-1)d)/2 = n(2 + (n-1)2)/2 = n(1 + n - 1) = n^2 So, the sum of the first n odd numbers is n^2 . But using standard formulae is annoying, so how about trying a little induction. sum_odd(1) = 1 sum_odd(n+1) = sum_odd(n) + (2n + 1) = n^2 + 2n + 1 = (n+1)^2 But back to the airline seat. Here's what I saw (I added the numbering, Lufthansa isn't kind enough to do that for you :-): The other thing I noticed was this:

Tonight, I'm going to write myself an Aston Martin

This is the story of my attempt to 'cheat' in an on-line spot-the-ball competition to win an Aston Martin . It's also the story of my failure, but you get free source code that implements automatic detection of image alteration using copy/paste or tools like the Clone Tool in Photoshop. First, take a look at this photo: Notice anything strange? In fact this image has been tampered with to cover up a truck. The truck is completely hidden by foliage. Here's the original: Wouldn't it be nice to be able to detect that automatically? It is possible. Here's an image automatically generated by my code showing what was moved. All of the red was moved to the blue (or the other way around). I was motivated to work on this program by greed (or at least my never-ending love of having a little flutter on things). Best of the Best runs spot-the-ball competitions in airports to win very expensive cars. But they also run the same competition online. Tha

Interface to SQLite database in 23 lines of Arc

One thing that the first release of Arc was missing was access to any sort of database, but that's easily remedied. Here are 23 lines of Arc code that provide access to a SQLite database: (= db! 'nil) (def db+ (name (o host "localhost") (o port 49153)) (let (i o) (connect-socket host port) (db> o name) (if (db< i) (list i o)))) (def sql ((i o) q) (db> o q) (if (db< i) (readall i 200))) (def db- (db) (map close db)) (def db> (o s) (write s o) (writec #\return o) (writec #\newline o) (flush-socket o)) (def db< (i) (= db! (read i)) (iso db! 200)) The three functions you need to care about are db+ (get a connection to a named SQLite database), db- (close a connection to a database) and sql (execute a SQL query and return a list (or lists) of rows. There's also db! which contains the status of the last command (200 for OK, or 500 followed by a string explaining the error). Here's a little Arc session creating a dat

My first Arc project: a simple Wiki

The only way to learn a programming language is to write something in it. So, I decided it was time to dig into Arc and my first project is a very simple Wiki. Here's the source ( wiki.arc ): ; A wiki written in Arc (arc0) ; ; Copyright (c) 2008 John Graham-Cumming ; ; (load "wiki.arc") ; (wsv) ; ; Then go to http://localhost:8080/show (load "web.arc") (load "util.arc") (= pagedir* "wiki/") (def histfiles (page) (sort > (map [coerce _ 'int] (rem [is "current" _] (dir (pagepath page)))))) (def nexthist (page) (let h (histfiles page) (if h (++ (car h)) 0))) (def pagepath (page) (string pagedir* (page 0) "/" (page 0) (page 1) "/" page )) (def pagefile (page (o file)) (string (pagepath page) "/" (or file "current"))) (def slurp (page (o file)) (if (let p (pagefile page file) (if (file-exists p) (readfile p))))) (def upperlen (word) (len (keep upper word)))

The Arc Challenge Explained

When I first looked at the Arc Challenge code my reaction, like that of many people, was WTH? Here's the code: (defop said req (aform [w/link (pr "you said: " (arg _ "foo")) (pr "click here")] (input "foo") (submit))) Within the context of the Arc web/app server this creates a page called /said which has a form on it: <form method=post action="x"> <input type=hidden name="fnid" value="JtCw8ju328"> <input type=text name="foo" value="" size=10> <input type=submit value="submit"> </form> That form accepts a single parameter called foo and redirects to /x . When clicking submit the user is taken to a page with a single link on it: <a href="x?fnid=bHJpJ5G1DH">click here</a> Following that link brings up a page showing what you typed in the first; here's the output when I typed hello in the