函数名为is-prime吗?它接受输入n,如果n为,则返回true 素数,否则为false。此函数应检查n是否为1或2,并且 作出相应反应;如果没有,应该打电话给您的除数吗?。
(defn divides? [div n] (zero? (mod n div)))
(defn no-divisors? [n]
(->> (range 2 n)
(filter #(divides? % n))
empty?))
下面的问题代码
(defn is-prime? [n]
(and (> n 1) (not-any? (filter #(no-divisors? % n)) (range 2 n))))
/// current output///
(is-prime? 1)
=> false
(is-prime? 2)
=> true
(is-prime? 3)
=> false
(is-prime? 4)
=> false
(is-prime? 101)
=> false
/// current output///
/// required output ///
(is-prime? 1)
=> false
(is-prime? 2)
=> true
(is-prime? 3)
=> true
(is-prime? 4)
=> false
(is-prime? 101)
=> true
/// required output ///
将不胜感激
答案 0 :(得分:3)
no-divisors?
是多余的。
(letfn [(divides? [m n] (zero? (rem m n)))
(prime? [n] (and (< 1 n) (not-any? #(divides? n %) (range 2 n))))]
(prn (for [i (range 100)
:when (prime? i)] i)))