Skip to content

Instantly share code, notes, and snippets.

@LispyAriaro
Last active August 29, 2015 14:15
Show Gist options
  • Save LispyAriaro/487281fadebf4b8cca44 to your computer and use it in GitHub Desktop.
Save LispyAriaro/487281fadebf4b8cca44 to your computer and use it in GitHub Desktop.
;;; (a1)xb1 + (a2)xb2 + (a3)xb3 ......(an)xbn
; Enter your code here. Read input from STDIN. Print output to STDOUT
;
(defn my-round
[the-num num-dec-places]
(let [factor (Math/pow 10 num-dec-places)]
(/ (Math/round (* the-num factor)) factor)))
(defn get-y-at-x [as-lst bs-lst x-point]
(my-round (reduce + (for [counter (range (count as-lst))]
(* (as-lst counter)
(Math/pow x-point (bs-lst counter))))) 2))
(defn get-area
([as-lst bs-lst
lower-limit upper-limit interval]
(get-area as-lst bs-lst
lower-limit upper-limit
interval lower-limit 0))
([as-lst bs-lst
lower-limit upper-limit interval
x-current area-so-far]
(if (> x-current upper-limit)
area-so-far
(recur as-lst bs-lst lower-limit
upper-limit interval (+ x-current interval)
(+ area-so-far (* interval
(get-y-at-x as-lst bs-lst
x-current)))))))
;;; I notice the repetition
;;; I should write a macro for it
;;; but I'm tired now.
(defn get-volume
([as-lst bs-lst
lower-limit upper-limit interval]
(get-volume as-lst bs-lst
lower-limit upper-limit
interval lower-limit 0))
([as-lst bs-lst
lower-limit upper-limit interval
x-current vol-so-far]
(if (> x-current upper-limit)
vol-so-far
(recur as-lst bs-lst lower-limit
upper-limit interval (+ x-current interval)
(+ vol-so-far (* interval (Math/PI)
(Math/pow (get-y-at-x as-lst bs-lst
x-current) 2)))))))
(defn get-input []
(let [as (read-line)
bs (read-line)
limits (read-line)
as-strs (clojure.string/split as #" ")
bs-strs (clojure.string/split bs #" ")
limits-strs (clojure.string/split limits #" ")
as-nums (vec (for [will as-strs]
(Integer/parseInt will)))
bs-nums (vec (for [will bs-strs]
(Integer/parseInt will)))
limits-num (vec (for [will limits-strs]
(Integer/parseInt will)))]
(println (get-area as-nums bs-nums
(limits-num 0)
(limits-num 1) 0.01))
(println (get-volume as-nums bs-nums
(limits-num 0)
(limits-num 1) 0.01))))
(get-input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment