slovo/benchmarks/branch-loop/clojure/branch_loop.clj
2026-05-22 08:38:43 +02:00

34 lines
927 B
Clojure

(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(def loop-count 1000000)
(def expected-checksum 1185071)
(defn configured-loop-count []
(try
(let [line (read-line)
value (Integer/parseInt (.trim ^String line))]
(if (pos? value) value loop-count))
(catch Exception _
loop-count)))
(defn branch-loop [limit]
(loop [i 0
acc 7]
(if (< i limit)
(let [bounded (if (> acc 1000000000)
(- acc 1000000000)
acc)
branched (if (< i 500000)
(+ bounded 3)
(+ bounded 7))
next (if (> branched 1234567)
(- branched 1234567)
(+ branched 11))]
(recur (inc i) next))
acc)))
(let [result (branch-loop (configured-loop-count))]
(println result)
(System/exit (if (= result expected-checksum) 0 1)))