Skip to content

Commit

Permalink
Add unfinished solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Nov 6, 2024
1 parent d90d19c commit 29171fa
Show file tree
Hide file tree
Showing 7 changed files with 3,652 additions and 27 deletions.
74 changes: 57 additions & 17 deletions solutions/beecrowd/1032/1032.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,91 @@

(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))))))
(not-any? #(divisible? number %) (range
3
(-> (Math/sqrt number)
(Math/ceil)
(int)
(inc))
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)))))))

(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))))))

(defn josephus [n]
(loop [current-n 2
k 2
(loop [current-n 1
;; index-prime 0
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))))))
(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))))))

(main)


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

;; (print-primes)


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

;; (print-nth-primes)
21 changes: 18 additions & 3 deletions solutions/beecrowd/1032/1032.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,24 @@ def next_prime(number):
return primes[number]


def josephus(n, k=2) -> int:
def josephus(n, k=2, nth_prime=None) -> int:

if nth_prime is None:
nth_prime = n
result = None
if n == 1:
return 1
return ((josephus(n - 1, next_prime(k)) + k - 1) % n) + 1
result = 1
else:
result = ((josephus(n - 1, next_prime(k), nth_prime) + k - 1) % n) + 1

# print(f'nth_prime = {nth_prime - n}')
print(f'k = {k}')
print(f'n = {n}')

print(f'result = {result}')
print('--')

return result


for line in sys.stdin:
Expand All @@ -68,3 +82,4 @@ def josephus(n, k=2) -> int:
break

print(josephus(n))
print('--------------')
Loading

0 comments on commit 29171fa

Please sign in to comment.