Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created February 16, 2010 13:26
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 francoisdevlin/305521 to your computer and use it in GitHub Desktop.
Save francoisdevlin/305521 to your computer and use it in GitHub Desktop.
(import [java.math BigInteger])
(defn conway-PM []
(let [factors [17/91 78/85 19/51 23/38 29/33 77/29 95/23
77/19 1/17 11/13 13/11 15/14 15/2 55/1]
big-int (fn [n];"makes a bigint from int, if needed"
(if (instance? BigInteger n)
n (BigInteger/valueOf n)))
special-log (fn [n]; base 2 log of n iff n = 2^m, else nil
(let [bn (big-int n)]
(if (= (.bitCount #^BigInteger bn) 1)
(- (.bitLength #^BigInteger bn) 1))))
step ;"Multiplies with the first factor to give an integer."
(fn [n, unused-factors]
(let [factor (first unused-factors)
rest-factors (rest unused-factors)
dummy (* n factor)]
(if (integer? dummy)
dummy
(recur n rest-factors))))]
(iterate #(step % factors) 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment