kawas <laurent.joi...@gmail.com> writes:

Hi Laurent,

> (defn prime-factors [n]
>   (loop [f 2 n n res []]
>     (cond
>       (<= n 1) res
>       (zero? (rem n f)) (recur f (quot n f) (conj res f))
>       :else (recur (inc f) n res))))
>
> Problem 1 (solved): If you use (= n 1) in the first cond clause, the
> function may not terminate because (= 1 1.0) may be false. But we all
> know that :)

Use (== n 1) instead.

> Problem 2: Now compare theses function calls
> (prime-factors (Math/pow 2 58))
> (prime-factors (bigdec (Math/pow 2 58)))
> (prime-factors (bigint (bigdec (Math/pow 2 58))))
>
> The last two function calls are really really slow.
> - Is this only a problem of rem/quot slow on big numbers ?

No, debugging it a bit it seems to be a bug in
clojure.lang.Numbers.quotient() applied to BigDecs:

user> (quot 1.4411518807585587E17 2)   ;; correct with doubles
7.2057594037927936E16
user> (quot 1.4411518807585587E+17M 2) ;; wrong with BigDecs
72057594037927935M

Please file a bug report about that.

Bye,
Tassilo

-- 
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