Skip to content

Instantly share code, notes, and snippets.



Last active Dec 15, 2015
What would you like to do?
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.1234567891011121…
(ns transcendental-numbers.champernownes-constant
(:use [transcendental-numbers.utils :only [digits integers]]))
(def digit-seq
(mapcat digits integers))
(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]
(fill-style (get colors digit))
(rect {:x x :y y :w size :h size}))))
(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)]
(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))
(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)]
(if (and (zero? c) (zero? r))
(cons x (sqrt0 (next xs) r p))))))
(defn digit-seq [n]
(let [xs (number-pairs n)]
(sqrt0 xs 0 0)))
(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)
(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]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.