Skip to content

Instantly share code, notes, and snippets.

@jl2
Created January 1, 2019 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jl2/a7afb7fb6de35a3a5943d27c57b22f35 to your computer and use it in GitHub Desktop.
Save jl2/a7afb7fb6de35a3a5943d27c57b22f35 to your computer and use it in GitHub Desktop.
(declaim (ftype (function (fixnum function) ) triples))
(time
(defun triples (n func)
(declare (optimize (speed 3) (debug 0) (safety 0)))
(declare (type function func)
(type fixnum n))
(let ((count 0))
(declare (type fixnum count))
(loop for z fixnum from 1 do
(loop for x fixnum from 1 to z do
(loop for y fixnum from x to z
when (= (the fixnum (* z z)) (the fixnum (+ (the fixnum (* x x)) (the fixnum (* y y)))))
do
(incf count)
(funcall func x y z)
(when (= count n)
(return-from triples))))))))
;; WARNING: redefining COMMON-LISP-USER::TRIPLES in DEFUN
;; Evaluation took:
;; 0.000 seconds of real time
;; 0.000069 seconds of total run time (0.000069 user, 0.000000 system)
;; 100.00% CPU
;; 175,800 processor cycles
;; 0 bytes consed
(declaim (inline show-triple)
(ftype (function (fixnum fixnum fixnum) ) show-triple))
(flet ((show-triple (x y z)
(format t "(~a ~a ~a)~%" x y z)))
(time (triples 1000 #'show-triple)))
;; Output:
;; (3 4 5)
;; (6 8 10)
;; (5 12 13)
;; ...
;; (460 1008 1108)
;; Evaluation took:
;; 0.227 seconds of real time
;; 0.228212 seconds of total run time (0.226570 user, 0.001642 system)
;; 100.44% CPU
;; 588,429,484 processor cycles
;; 659,376 bytes consed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment