Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Almost-prime/Clojure/almost-prime.clj
Normal file
23
Task/Almost-prime/Clojure/almost-prime.clj
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(ns clojure.examples.almostprime
|
||||
(:gen-class))
|
||||
|
||||
(defn divisors [n]
|
||||
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
|
||||
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
|
||||
(if div ; div = nil (if no divisor found else its the divisor)
|
||||
(into [] (concat (divisors div) (divisors (/ n div)))) ; Concat the two divisors of the two divisors
|
||||
[n]))) ; Number is prime so only itself as a divisor
|
||||
|
||||
(defn divisors-k [k n]
|
||||
" Finds n numbers with k divisors. Does this by looping through integers 2, 3, ... filtering (passing) ones with k divisors and
|
||||
taking the first n "
|
||||
(->> (iterate inc 2) ; infinite sequence of numbers starting at 2
|
||||
(map divisors) ; compute divisor of each element of sequence
|
||||
(filter #(= (count %) k)) ; filter to take only elements with k divisors
|
||||
(take n) ; take n elements from filtered sequence
|
||||
(map #(apply * %)))) ; compute number by taking product of divisors
|
||||
|
||||
(println (for [k (range 1 6)]
|
||||
(println "k:" k (divisors-k k 10))))
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue