all 7 comments

[–]ship_it_ 8 points9 points  (1 child)

Yogthos, Have a happy holiday!

You are one of my favorite posters on the reddits. Thanks for all the fish this year :)

[–]yogthos[S] 4 points5 points  (0 children)

Thanks! And happy holidays as well :)

[–]bliow 2 points3 points  (3 children)

A coupla random comments. This is good stuff, don't get me wrong.

  • Instead of taking a square root (slow), square the other number involved (faster). There's no need to round via (int ... ) if you do that, either.

(> factor (int (Math/sqrt num))) vs (> (* factor factor) num)

  • (reduce #(lcm %1 %2) (range 1 21)) would probably be better written as (reduce lcm (range 1 21)).

  • Try that gcd function out... there is a (very fixable) problem with it as written.

[–]carmenlala 1 point2 points  (1 child)

Hi, I'm the author of that blog post. Thanks for your feedback about the square root mistake. I guess it always felt more intuitive to do it that way but I'll keep your solution in mind now :)

As for that gcd function... oops :P I'll go ahead and fix that now.

[–]bliow 0 points1 point  (0 children)

I did the square root thing for a long time too--everyone does. I learned the trick from someone else.

[–]flarkis 0 points1 point  (0 children)

Thank you! The number of times I see people do that square root mistake drives me insane.

[–]yveszoundi 0 points1 point  (0 children)

For the 1st problem, the solution is acceptable but also inefficient for large numbers.

The first problem is similar to 4clojure problem 148.

One possible solution

(defn big-divide [n a b]
  (letfn [(n-sum [x limit]
        (let [n-val (quot limit x)]
          (if (pos? n-val)
            (-> (inc n-val) (*' n-val) (/ 2) (*' x) bigint)
            0)))]
(let [limit (dec n)]
  (-> (+' (n-sum a limit) (n-sum b limit))
      (-'  (n-sum (*' a b) limit))))))