Skip to content

Commit

Permalink
Add unfinished solution
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Oct 23, 2024
1 parent a9644b1 commit 2215695
Show file tree
Hide file tree
Showing 6 changed files with 7,059 additions and 6,983 deletions.
55 changes: 55 additions & 0 deletions solutions/beecrowd/1032/1032.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(ns main)

(defn divisible? [number divisor]
(zero? (rem number divisor)))

(def prime?
(memoize (fn [number]
;; (printf "(prime? %d)%n" number)
(cond (= number 2)
true
(or (< number 2) (divisible? number 2))
false
:else
(not-any? #(divisible? number %) (range 3 (+ (Math/sqrt number) 1) 2))))))

(def next-prime
(memoize (fn [number]
;; (printf "(next-prime %d)%n" number)
(let [next-number (inc number)]
;; (printf "next-number: %d%n" next-number)
(if (prime? next-number)
next-number
(next-prime (inc number)))))))

(defn josephus [n]
(loop [current-n 2
k 2
result 1]
;; (printf "result %d%n" result)
(if (> current-n n)
result
(recur (inc current-n)
(next-prime k)
(-> result
(+ k)
(dec)
(mod current-n)
(inc))))))

(defn main []
(loop [number (Integer/parseInt (read-line))]
(when (not= number 0)
(printf "%d%n" (josephus number))
(recur (Integer/parseInt (read-line))))))

(main)


(defn print-primes []
(loop [n 2]
(when (<= n 200)
(printf "%d -> %d%n" n (next-prime n))
(recur (next-prime n)))))

;; (print-primes)
Empty file added solutions/beecrowd/1032/WRONG
Empty file.
Loading

0 comments on commit 2215695

Please sign in to comment.