The same strategy works in Clojure, of course:
(defn b-of-a [a] (/ (* 1000.0 (- 500.0 a)) (- 1000.0 a)))
(defn near-int? [x] (< (- x (floor x)) 0.00001))
(first (for [a (iterate inc 1) :let [b (b-of-a a)] :when (near-int? b)]
[a b (sqrt (+ (* a a) (* b b)))]))
Stu
I like Clojure, but as a point of comparison, here's a Haskell
solution, as typed in the REPL:
Prelude> let bOf a = 1000*(500 - a)/(1000 - a)
Prelude> let nearInt x = x - fromInteger(truncate x) < 0.000001
Prelude> head [ ( a, b, sqrt(a^2 + b^2) ) | a <- [1..], b <- [bOf a],
nearInt b ]
(200.0,375.0,425.0)
The numbers in the result add up to 1000, of course. Here, I just
solved b in terms of a, which is function bOf. Predicate nearInt
detects whether its argument is an integer (or close enough). Haskell
is lazy, so even though [1..] and the big list comprehension are
infinite, head just needs the first element.
--
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
--
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