0 Replies - 1207 Views - Last Post: 20 April 2010 - 01:01 PM

#1 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

[Lisp] Check if a Number is Prime

Posted 20 April 2010 - 01:01 PM

Description: Checks if a number is prime. Could be faster, but runs quickly (confirms primes > 800,000 in 2ms on a 7 year old computer), and is easy to understand
(defn prime?
  [num]
  (cond
   (even? num) false ; return false if even
   (some #(= (rem num %) 0) (range 3 (inc (int (Math/sqrt num))) 2)) false
   :else true))

Is This A Good Question/Topic? 0
  • +

Page 1 of 1