Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created May 9, 2011 16:58
Show Gist options
  • Select an option

  • Save swannodette/962877 to your computer and use it in GitHub Desktop.

Select an option

Save swannodette/962877 to your computer and use it in GitHub Desktop.

Revisions

  1. swannodette revised this gist May 10, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    (defn count-primes [^long n]
    (let [c (inc n)
    ^"[Z" prime? (make-array Boolean/TYPE c)]
    ^booleans prime? (make-array Boolean/TYPE c)]
    (iloop [(i 2) (<= i n) (inc i)]
    (aset prime? i true))
    (iloop [(i 2) (<= (* i i) n) (inc i)]
  2. swannodette revised this gist May 9, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    (set! *unchecked-math* true)

    (defmacro iloop [[b t n] & body]
    `(loop [~@b]
    (when ~t
  3. swannodette revised this gist May 9, 2011. 1 changed file with 1 addition and 14 deletions.
    15 changes: 1 addition & 14 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,3 @@
    (ns clj-play.seive)

    (set! *warn-on-reflection* true)
    (set! *unchecked-math* true)

    (defmacro iloop [[b t n] & body]
    `(loop [~@b]
    (when ~t
    @@ -21,12 +16,4 @@
    (areduce prime? i r 0
    (if (aget prime? i)
    (inc r)
    r))))

    (comment
    ;; 2.8-2.9s same as Java w/ long arithmetic
    (do
    (dotimes [_ 10]
    (time
    (count-primes 1e8))))
    )
    r))))
  4. swannodette revised this gist May 9, 2011. No changes.
  5. swannodette revised this gist May 9, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@
    r))))

    (comment
    ;; 2.8-2.9s same as Java
    ;; 2.8-2.9s same as Java w/ long arithmetic
    (do
    (dotimes [_ 10]
    (time
  6. swannodette revised this gist May 9, 2011. 1 changed file with 5 additions and 10 deletions.
    15 changes: 5 additions & 10 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -18,18 +18,13 @@
    (if (aget prime? i)
    (iloop [(j i) (<= (* i j) n) (inc j)]
    (aset prime? (* i j) false))))
    (let [primes (loop [i 2 c 0]
    (if (<= i n)
    (let [ni (inc i)
    nc (if (aget prime? i)
    (inc c)
    c)]
    (recur ni nc))
    c))]
    primes)))
    (areduce prime? i r 0
    (if (aget prime? i)
    (inc r)
    r))))

    (comment
    ;; ~2.8s same as Java w/ long arithmetic
    ;; 2.8-2.9s same as Java
    (do
    (dotimes [_ 10]
    (time
  7. swannodette revised this gist May 9, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    (iloop [(j i) (<= (* i j) n) (inc j)]
    (aset prime? (* i j) false))))
    (let [primes (loop [i 2 c 0]
    (if (lte i n)
    (if (<= i n)
    (let [ni (inc i)
    nc (if (aget prime? i)
    (inc c)
  8. swannodette revised this gist May 9, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@
    primes)))

    (comment
    ;; 2s
    ;; ~2.8s same as Java w/ long arithmetic
    (do
    (dotimes [_ 10]
    (time
  9. swannodette revised this gist May 9, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -15,13 +15,13 @@
    (iloop [(i 2) (<= i n) (inc i)]
    (aset prime? i true))
    (iloop [(i 2) (<= (* i i) n) (inc i)]
    (if (identical? (aget prime? i) true)
    (if (aget prime? i)
    (iloop [(j i) (<= (* i j) n) (inc j)]
    (aset prime? (* i j) false))))
    (let [primes (loop [i 2 c 0]
    (if (lte i n)
    (let [ni (inc i)
    nc (if (identical? (aget prime? i) true)
    nc (if (aget prime? i)
    (inc c)
    c)]
    (recur ni nc))
  10. swannodette revised this gist May 9, 2011. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,6 @@
    ~@body
    (recur ~n))))

    (defmacro lte [x y]
    `(. clojure.lang.Numbers (lte ~x ~y)))

    (defn count-primes [^long n]
    (let [c (inc n)
    ^"[Z" prime? (make-array Boolean/TYPE c)]
  11. swannodette created this gist May 9, 2011.
    40 changes: 40 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    (ns clj-play.seive)

    (set! *warn-on-reflection* true)
    (set! *unchecked-math* true)

    (defmacro iloop [[b t n] & body]
    `(loop [~@b]
    (when ~t
    ~@body
    (recur ~n))))

    (defmacro lte [x y]
    `(. clojure.lang.Numbers (lte ~x ~y)))

    (defn count-primes [^long n]
    (let [c (inc n)
    ^"[Z" prime? (make-array Boolean/TYPE c)]
    (iloop [(i 2) (<= i n) (inc i)]
    (aset prime? i true))
    (iloop [(i 2) (<= (* i i) n) (inc i)]
    (if (identical? (aget prime? i) true)
    (iloop [(j i) (<= (* i j) n) (inc j)]
    (aset prime? (* i j) false))))
    (let [primes (loop [i 2 c 0]
    (if (lte i n)
    (let [ni (inc i)
    nc (if (identical? (aget prime? i) true)
    (inc c)
    c)]
    (recur ni nc))
    c))]
    primes)))

    (comment
    ;; 2s
    (do
    (dotimes [_ 10]
    (time
    (count-primes 1e8))))
    )