RosettaCodeData/Task/Bernoulli-numbers/Clojure/bernoulli-numbers.clj
2023-07-01 13:44:08 -04:00

24 lines
1 KiB
Clojure
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

ns test-project-intellij.core
(:gen-class))
(defn a-t [n]
" Used Akiyama-Tanigawa algorithm with a single loop rather than double nested loop "
" Clojure does fractional arithmetic automatically so that part is easy "
(loop [m 0
j m
A (vec (map #(/ 1 %) (range 1 (+ n 2))))] ; Prefil A(m) with 1/(m+1), for m = 1 to n
(cond ; Three way conditional allows single loop
(>= j 1) (recur m (dec j) (assoc A (dec j) (* j (- (nth A (dec j)) (nth A j))))) ; A[j-1] ← j×(A[j-1] - A[j]) ;
(< m n) (recur (inc m) (inc m) A) ; increment m, reset j = m
:else (nth A 0))))
(defn format-ans [ans]
" Formats answer so that '/' is aligned for all answers "
(if (= ans 1)
(format "%50d / %8d" 1 1)
(format "%50d / %8d" (numerator ans) (denominator ans))))
;; Generate a set of results for [0 1 2 4 ... 60]
(doseq [q (flatten [0 1 (range 2 62 2)])
:let [ans (a-t q)]]
(println q ":" (format-ans ans)))