Primality by trial division: Difference between revisions

Line 647:
 
=={{header|Clojure}}==
<lang clojure>(defn divides? [k n] (=zero? (remmod k n k) 0))
The symbol # is a shortcut for creating lambda functions; the arguments in such a function are %1, %2, %3... (or simply % if there is only one argument). Thus, #(< (* % %) n) is equivalent to (fn [x] (< (* x x) n)) or more mathematically f(x) = x * x < n.
<lang clojure>(defn divides? [k n] (= (rem n k) 0))
 
(defn prime? [nx]
(ifor (< n= 2 x)
false (= 3 x)
(and (< 1 x)
(empty? (filter #(divides? % n) (take-while #(<= (* % %) n) (range 2 n))))))</lang>
(odd? x)
(not-any? divides? (range 3 (math/sqrt x) 2)))))</lang>
 
=={{header|CMake}}==