Skip to content

Instantly share code, notes, and snippets.

@rm-hull
Last active December 15, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rm-hull/5233367 to your computer and use it in GitHub Desktop.
Save rm-hull/5233367 to your computer and use it in GitHub Desktop.
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]
(->
enchilada/ctx
(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)]
(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)))
(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]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment