Hi,

I just finished my first (very small) Clojure program. I come from
Java, so it will be everything but idiomatic Clojure, so I post it
here and you may give as much criticism as you want. I'm here to
learn.

It implements JHConway prime machine. The program does not want to be
a quick prime generator (the algorithm is interesting but not quick at
all), but it shall be/become a good implementation of this algorithm.

http://www.jstor.org/pss/2690263 (The first page is enough to
implement the algorithm, even if the explanation will come on the next
pages only.)

Started like:

(conway-PM)

It came up to 563 after about 6 hours on my 1.2 GHz Centrino ;-)

Thats the program, feel free to tell me stuff (or to ignore me, would
be a lessen too :)

(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-class (. 999999999999999999999999999999999 getClass)]
                (letfn [
                        (big-int [n];"makes a bigint from int, keeps n if 
bigint already"
                                (let [current-class (. n getClass)]
                                        (if (= current-class big-int-class) n
                                                (. java.math.BigInteger valueOf 
n))))
                        (power-of-two? [n];"true iff the integer n is an power 
of 2."
                                (= (. (big-int n) bitCount) 1))
                        (what-power-of-2 [n];"gets n from 2^n"
                                (- (. (big-int n) bitLength) 1))
                        (step ;"Multiplies with the first factor to give an 
integer."
                                [n, unused-factors]
                                (let [factor (first unused-factors)
                                          rest-factors (rest unused-factors)
                                          dummy (* n factor)]
                                (if (integer? dummy)
                                        dummy
                                        (step n rest-factors))))]
                        (loop [seed 2]
                                (let [next (step seed factors)]
                                        (if (power-of-two? next)
                                                (println (what-power-of-2 
next)))
                                        (recur next))))))

Regards, alux

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to