forked from Sethathi/SBITC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer1.clj
34 lines (30 loc) · 811 Bytes
/
answer1.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(ns answer1)
(defn prime?
{:doc "Returns a number one greater than x"}
[n]
(let [r (int (Math/sqrt n))]
(loop [d 2]
(cond (= n 1) false
(> d r) true
(zero? (rem n d)) false
:else (recur (inc d))))))
(defn prime-count
{:doc "Prints test case number (t) followed by number of primes for n"}
[t n]
(def p 0)
(doseq [i (range 1 (+ n 1))]
(when (prime? i)
(def p (inc p))))
(str "Case #" t ": " p "\n"))
(def output "")
(def T (Integer/parseInt (read-line)))
(def T (cond (< T 1) 1
(> T 20) 20
:else T))
(doseq [t (range 1 (+ T 1))]
(def N (Integer/parseInt (read-line)))
(def N (cond (< N 1) 1
(> N 10000) 10000
:else N))
(def output (str output (prime-count t N))))
(print output)