Common Lispでコンパイル時の素数判定計算

  • 3
    Stock
  • 0
    Comment
Stock this post
  • stibear
  • phonohawk
  • guicho271828

constexpr メタプログラミング:「速度が欲しいか… ならばくれてやる!」
http://zakkas783.tumblr.com/post/7842759252/constexpr
を読んで、Common Lispもコンパイル時の計算が得意だなと思って書いてみた

(defun primep (n)
  #+sbcl (sb-int:positive-primep n))

;; (primep 4461926290642603313185756658254841232481376192090294129197)
;; ....

(define-compiler-macro primep (&whole form n)
  (if (integerp n)
      (and (ppcre:scan "is a prime number"
                       (drakma:http-request
                        (format nil
                                "http://www.wolframalpha.com/input/?i=~D"
                                n)))
           'T)
      form))

(defun prime-test ()
  (list :primep
        4461926290642603313185756658254841232481376192090294129197
        '=>
        (primep 4461926290642603313185756658254841232481376192090294129197)))

(prime-test)
;⇒ (:PRIMEP 4461926290642603313185756658254841232481376192090294129197 => T)
----------
Evaluation took:
  0.000 seconds of real time
  0.000000 seconds of total run time (0.000000 user, 0.000000 system)
  100.00% CPU
  2,448 processor cycles
  0 bytes consed

Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.