-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
108faf9
commit 6a66019
Showing
7 changed files
with
89 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,8 @@ | |
[a b] | ||
(-> (- a b) | ||
Math/abs | ||
(+ b) | ||
(+ a) | ||
(/ 2))) | ||
(+ b a) | ||
(/ 2))) | ||
|
||
(defn main [] | ||
(loop [line (read-line)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,95 @@ | ||
(ns main) | ||
;; (ns main) | ||
|
||
(defn divisible? [number divisor] | ||
(zero? (rem number divisor))) | ||
;; (defn divisible? [number divisor] | ||
;; (zero? (rem number divisor))) | ||
|
||
(def prime? | ||
(memoize (fn [number] | ||
(cond (= number 2) | ||
true | ||
(or (< number 2) (divisible? number 2)) | ||
false | ||
:else | ||
(not-any? #(divisible? number %) (range | ||
3 | ||
(-> (Math/sqrt number) | ||
Math/ceil | ||
int | ||
inc) | ||
2)))))) | ||
;; (def prime? | ||
;; (memoize (fn [number] | ||
;; (cond (= number 2) | ||
;; true | ||
;; (or (< number 2) (divisible? number 2)) | ||
;; false | ||
;; :else | ||
;; (not-any? #(divisible? number %) (range | ||
;; 3 | ||
;; (-> (Math/sqrt number) | ||
;; Math/ceil | ||
;; int | ||
;; inc) | ||
;; 2)))))) | ||
|
||
(def next-prime | ||
(memoize (fn [number] | ||
(let [next-number (inc number)] | ||
(if (prime? next-number) | ||
next-number | ||
(next-prime (inc number))))))) | ||
;; (def next-prime | ||
;; (memoize (fn [number] | ||
;; (let [next-number (inc number)] | ||
;; (if (prime? next-number) | ||
;; next-number | ||
;; (next-prime (inc number))))))) | ||
|
||
(def nth-prime | ||
(memoize (fn [nth] | ||
(loop [index 0 | ||
prime 2] | ||
(cond (= nth 0) | ||
2 | ||
(= index (dec nth)) | ||
(next-prime prime) | ||
:else | ||
(recur (inc index) | ||
(next-prime prime))))))) | ||
;; (def nth-prime | ||
;; (memoize (fn [nth] | ||
;; (loop [index 0 | ||
;; prime 2] | ||
;; (cond (= nth 0) | ||
;; 2 | ||
;; (= index (dec nth)) | ||
;; (next-prime prime) | ||
;; :else | ||
;; (recur (inc index) | ||
;; (next-prime prime))))))) | ||
|
||
;; (def josephus | ||
;; (memoize (fn [n k] | ||
;; (if (= n 1) | ||
;; 1 | ||
;; (-> (josephus (dec n) (next-prime k)) | ||
;; (+ k) | ||
;; (dec) | ||
;; (mod n) | ||
;; (inc)))))) | ||
;; ;; (def josephus | ||
;; ;; (memoize (fn [n k] | ||
;; ;; (if (= n 1) | ||
;; ;; 1 | ||
;; ;; (-> (josephus (dec n) (next-prime k)) | ||
;; ;; (+ k) | ||
;; ;; (dec) | ||
;; ;; (mod n) | ||
;; ;; (inc)))))) | ||
|
||
(defn josephus [n] | ||
(loop [current-n 1 | ||
;; index-prime 0 | ||
result 1] | ||
(printf "prime: %d > %d%n" (- n current-n) (nth-prime (- n current-n))) | ||
(printf "current-n: %d%n" current-n) | ||
(printf "result: %d%n" result) | ||
(printf "--%n") | ||
(cond (= n 1) | ||
1 | ||
(= current-n (inc n)) | ||
result | ||
:else | ||
(recur (inc current-n) | ||
;; (inc index-prime) | ||
(-> result | ||
(+ (nth-prime (- n current-n))) | ||
dec | ||
(mod current-n) | ||
inc)))) | ||
;; (defn josephus [n] | ||
;; (loop [current-n 1 | ||
;; ;; index-prime 0 | ||
;; result 1] | ||
;; (printf "prime: %d > %d%n" (- n current-n) (nth-prime (- n current-n))) | ||
;; (printf "current-n: %d%n" current-n) | ||
;; (printf "result: %d%n" result) | ||
;; (printf "--%n") | ||
;; (cond (= n 1) | ||
;; 1 | ||
;; (= current-n (inc n)) | ||
;; result | ||
;; :else | ||
;; (recur (inc current-n) | ||
;; ;; (inc index-prime) | ||
;; (-> result | ||
;; (+ (nth-prime (- n current-n))) | ||
;; dec | ||
;; (mod current-n) | ||
;; inc)))) | ||
|
||
(defn main [] | ||
(loop [number (Integer/parseInt (read-line))] | ||
(printf "-----------%n") | ||
(when (not= number 0) | ||
(printf "%d%n" (josephus number)) | ||
(recur (Integer/parseInt (read-line)))))) | ||
;; (defn main [] | ||
;; (loop [number (Integer/parseInt (read-line))] | ||
;; (printf "-----------%n") | ||
;; (when (not= number 0) | ||
;; (printf "%d%n" (josephus number)) | ||
;; (recur (Integer/parseInt (read-line)))))) | ||
|
||
(main) | ||
;; (main) | ||
|
||
(defn print-primes [] | ||
(loop [n 2] | ||
(when (<= n 200) | ||
(printf "%d -> %d%n" n (next-prime n)) | ||
(recur (next-prime n))))) | ||
;; (defn print-primes [] | ||
;; (loop [n 2] | ||
;; (when (<= n 200) | ||
;; (printf "%d -> %d%n" n (next-prime n)) | ||
;; (recur (next-prime n))))) | ||
|
||
;; (print-primes) | ||
;; ;; (print-primes) | ||
|
||
|
||
(defn print-nth-primes [] | ||
(loop [n 0] | ||
(when (<= n 200) | ||
(printf "%d -> %d%n" n (nth-prime n)) | ||
(recur (inc n))))) | ||
;; (defn print-nth-primes [] | ||
;; (loop [n 0] | ||
;; (when (<= n 200) | ||
;; (printf "%d -> %d%n" n (nth-prime n)) | ||
;; (recur (inc n))))) | ||
|
||
;; (print-nth-primes) | ||
;; ;; (print-nth-primes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ | |
(defn delta [a b c] | ||
(-> (* b b) | ||
(- 4) | ||
(* a) | ||
(* c))) | ||
(* a c))) | ||
|
||
(defn bhaskara [a b delta] | ||
[(-> b | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters