Champernowne's Constant and other transcendentals

mathcanvas

Experiments in colourizing digits of transcendental numbers like √2, π and Champernowne's constant - numbers which are not the root of any polynomial with integer coeffients. Whilst an Cambridge undergraduate in 1933, David Champernowne concatenated the positive integers, 1, 2, 3, 4, ..., and lead with a decimal point to yield 0.123456789101112131415161718192021... He showed that the frequency of any single digit is exactly 10 percent, that the frequency of any two digit pairs is one percent, and three digit pairs have a frequency of 0.1%, and so on. Because of this statistical property, it might appear random and it would be easy to thwart attempts to find sequences, and overlook the "regularity" within the sequence. Plotting the sequence with each digit colour-coded as below shows there is a pattern of sorts. See http://www.matifutbol.com/en/pencil.html for some more information.

24 Mar 2013 by rm-hull
rm-hull/5233367
champernowne.cljs
(ns transcendental-numbers.champernownes-constant
  (:use [transcendental-numbers.utils :only [digits integers]]))

(def digit-seq 
  (mapcat digits integers))
main.cljs
(ns transcendental-numbers.demo
  (:use [monet.canvas :only [fill-style rect]]
        [jayq.core :only [show]]
        [enchilada :only [ctx canvas]])
  (:require [transcendental-numbers/utils :as utils]
            [transcendental-numbers/square-root :as sqrt]
            [transcendental-numbers/champernownes-constant :as champernowne]))

(def color-swatch 
  { :honeysuckle  "#E16889"
    :coral-rose   "#FE853E"
    :peapod       "#6EC59B"
    :beeswax      "#FDBA52"
    :silver-peony "#F5DED0"
    :russet       "#94614C"
    :regatta      "#2D97D3"
    :blue-curacao "#48C3CB" 
    :lavender     "#A9A6D3"
    :silver-cloud "#C0C1BC" })

(show canvas)

(let [size   4
      colors (vec (vals color-swatch))
      data   (map vector (utils/coords 800 600 :step size) champernowne/digit-seq)]
  (doseq [[[x y] digit] data]
    (-> 
      enchilada/ctx
      (fill-style (get colors digit))
      (rect {:x x :y y :w size :h size})))) 
square_root.cljs
(ns transcendental-numbers.square-root
  (:use [transcendental-numbers.utils :only [digits integers]]))

(defn number-pairs [n]
  (let [join (fn [[a b]] (+ (* 10 a) b))
        d (digits n)
        normalized (if (odd? (count d)) (cons 0 d) d)]
    (concat 
      (map join (partition 2 normalized))
      (repeat 0))))

(defn calc-y [x p]
  (* x (+ (* 20 p) x)))

(defn biggest-x [c p]
  (->> (iterate inc 0)
       (take-while #(<= (calc-y % p) c))
       last))

(defn- sqrt0 [xs r p]
  (let [c (+ (* r 100) (first xs))
        x (biggest-x c p)
        y (calc-y x p)
        r (- c y)
        p (+ (* 10 p) x)]
    (lazy-seq 
      (if (and (zero? c) (zero? r)) 
        nil 
        (cons x (sqrt0 (next xs) r p))))))

(defn digit-seq [n]
  (let [xs (number-pairs n)]
    (sqrt0 xs 0 0)))
utils.cljs
(ns transcendental-numbers.utils)

(def integers (iterate inc 1))

(defn digits 
  ([n] (digits n 10))
  ([n ^long radix]
    (loop [n n
           res nil]
      (if (zero? n)
        res
        (recur 
          (quot n radix)
          (cons (rem n radix) res))))))

(defn coords [w h & {:keys [step] :or {step 1}}]
  (for [y (range 0 h step)
        x (range 0 w step)]
    [x y]))