Inspired by the Game of Life tweet challenge (that was seriously cool, for a geeky definition of "cool"), I compressed a program that calculates pi to arbitrary precision. By my count, this is 132 characters, after removing comments and extra whitespace:

;; Computes a converging approximation of (atan x) with alternating
;; signs, if x is in (0,1]
(define (q x n)
  (if (= n 0)
      x
      (- (let ([y  (+ 1 (* n 2))])
           (/ (expt x y) y))
         (q x (- n 1)))))

;; Computes pi to arbitrary precision (higher n is better) using
;; Machin's formula
(define (pi n)
  (abs (- (* 16 (q 1/5 n))
          (* 4 (q 1/239 n)))))


For example, (real->decimal-string (pi 141) 200) computes pi to 200 decimal places.

What's your favorite not just computable, but tweetable function? :)

Neil ⊥
____________________
 Racket Users list:
 http://lists.racket-lang.org/users

Reply via email to