A   k-Almost-prime   is a natural number   



n


{\displaystyle n}

   that is the product of   



k


{\displaystyle k}

   (possibly identical) primes.



Example

1-almost-primes,   where   



k
=
1


{\displaystyle k=1}

,   are the prime numbers themselves.

2-almost-primes,   where   



k
=
2


{\displaystyle k=2}

,   are the   semiprimes.



Task

Write a function/method/subroutine/... that generates k-almost primes and use it to create a table here of the first ten members of k-Almost primes for   



1
<=
K
<=
5


{\displaystyle 1<=K<=5}

.



Related tasks

   Semiprime
   Category:Prime Numbers


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